Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets

I’m a little late to this party, but I think I have something useful to add. Kekoa’s answer is great but, as RonLugge mentions, it can make the button no longer respect sizeToFit or, more importantly, can cause the button to clip its content when it is intrinsically sized. Yikes! First, though, A brief explanation … Read more

preferredStatusBarStyle isn’t called

For anyone using a UINavigationController: The UINavigationController does not forward on preferredStatusBarStyle calls to its child view controllers. Instead it manages its own state – as it should, it is drawing at the top of the screen where the status bar lives and so should be responsible for it. Therefor implementing preferredStatusBarStyle in your VCs … Read more

How to find topmost view controller on iOS

I think you need a combination of the accepted answer and @fishstix’s + (UIViewController*) topMostController { UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController; while (topController.presentedViewController) { topController = topController.presentedViewController; } return topController; } Swift 3.0+ func topMostController() -> UIViewController? { guard let window = UIApplication.shared.keyWindow, let rootViewController = window.rootViewController else { return nil } var topController = … Read more

Changing Placeholder Text Color with Swift

You can set the placeholder text using an attributed string. Just pass the color you want to the attributes parameter. Swift 5: let myTextField = UITextField(frame: CGRect(x: 0, y: 0, width: 200, height: 30)) myTextField.backgroundColor = .blue myTextField.attributedPlaceholder = NSAttributedString( string: “Placeholder Text”, attributes: [NSAttributedString.Key.foregroundColor: UIColor.white] ) Swift 3: myTextField.attributedPlaceholder = NSAttributedString( string: “Placeholder Text”, … Read more

How to trap on UIViewAlertForUnsatisfiableConstraints?

This post helped me A LOT! I added UIViewAlertForUnsatisfiableConstraints symbolic breakpoint with suggested action: Obj-C project po [[UIWindow keyWindow] _autolayoutTrace] Swift project expr -l objc++ -O — [[UIWindow keyWindow] _autolayoutTrace] With this hint, the log became more detailed, and It was easier for me identify which view had the constraint broken. UIWindow:0x7f88a8e4a4a0 | UILayoutContainerView:0x7f88a8f23b70 | … Read more

How to programmatically get iOS status bar height

[UIApplication sharedApplication].statusBarFrame.size.height. But since all sizes are in points, not in pixels, status bar height always equals 20. Update. Seeing this answer being considered helpful, I should elaborate. Status bar height is, indeed, equals 20.0f points except following cases: status bar has been hidden with setStatusBarHidden:withAnimation: method and its height equals 0.0f points; as @Anton … Read more

How to capture UIView to UIImage without loss of quality on retina display

Switch from use of UIGraphicsBeginImageContext to UIGraphicsBeginImageContextWithOptions (as documented on this page). Pass 0.0 for scale (the third argument) and you’ll get a context with a scale factor equal to that of the screen. UIGraphicsBeginImageContext uses a fixed scale factor of 1.0, so you’re actually getting exactly the same image on an iPhone 4 as … Read more

Adjust UILabel height depending on the text

sizeWithFont constrainedToSize:lineBreakMode: is the method to use. An example of how to use it is below: //Calculate the expected size based on the font and linebreak mode of your label // FLT_MAX here simply means no constraint in height CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX); CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode]; //adjust the label the … Read more

UILabel text margin [duplicate]

I solved this by subclassing UILabel and overriding drawTextInRect: like this: – (void)drawTextInRect:(CGRect)rect { UIEdgeInsets insets = {0, 5, 0, 5}; [super drawTextInRect:UIEdgeInsetsInsetRect(rect, insets)]; } Swift 3.1: override func drawText(in rect: CGRect) { let insets = UIEdgeInsets.init(top: 0, left: 5, bottom: 0, right: 5) super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)) } Swift 4.2.1: override func drawText(in rect: CGRect) … Read more

Move view with keyboard using Swift

Here is a solution, without handling the switch from one textField to another: override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillShow:”), name: UIKeyboardWillShowNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector(“keyboardWillHide:”), name: UIKeyboardWillHideNotification, object: nil) } func keyboardWillShow(notification: NSNotification) { if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() { self.view.frame.origin.y -= keyboardSize.height } } func keyboardWillHide(notification: NSNotification) { self.view.frame.origin.y … Read more