AutoLayout, Unable to simultaneously satisfy constraints

The easiest way how to find unsatisfiable constraints: set unique identifier for every constraint in your view: create simple extension for NSLayoutConstraint: SWIFT: extension NSLayoutConstraint { override public var description: String { let id = identifier ?? “” return “id: \(id), constant: \(constant)” //you may print whatever you want here } } OBJECTIVE-C @interface NSLayoutConstraint … Read more

Multiline UIButton and autolayout

I had the same problem where I wanted my button to grow along with its title. I had to sublcass the UIButton and its intrinsicContentSize so that it returns the intrinsic size of the label. – (CGSize)intrinsicContentSize { return self.titleLabel.intrinsicContentSize; } Since the UILabel is multiline, its intrinsicContentSize is unknown and you have to set … Read more

UITextView is not scrolled to top when loaded

add the following function to your view controller class… Swift 3 override func viewDidLayoutSubviews() { self.mainTextView.setContentOffset(.zero, animated: false) } Swift 2.1 override func viewDidLayoutSubviews() { self.mainTextView.setContentOffset(CGPointZero, animated: false) } Objective C – (void)viewDidLayoutSubviews { [self.mainTextView setContentOffset:CGPointZero animated:NO]; }

iOS AutoLayout multi-line UILabel

There’s an answer this question on objc.io in the “Intrinsic Content Size of Multi-Line Text” section of Advanced Auto Layout Toolbox. Here’s the relevant info: The intrinsic content size of UILabel and NSTextField is ambiguous for multi-line text. The height of the text depends on the width of the lines, which is yet to be … Read more

Centering a view in its superview using Visual Format Language

Currently, no, it doesn’t look like it is possible to center a view in the superview using only VFL. It is, however, not that difficult to do it using a single VFL string and a single extra constraint (per axis): VFL: “|-(>=20)-[view]-(>=20)-|” [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:view.superview attribute:NSLayoutAttributeCenterX multiplier:1.f constant:0.f]; One would think that you … Read more

Loading a ViewController inside a Container View

You need to tell your BannerContainer view controller that it has a new child controller, and to tell the Child that it has a parent VC. This is described in the Apple Docs here. Like this: [self addChildViewController:vc]; vc.view.frame = CGRectMake(0, 0, self.container.frame.size.width, self.container.frame.size.height); [self.container addSubview:vc.view]; [vc didMoveToParentViewController:self]; Or in Swift: self.addChildViewController(vc) vc.view.frame = CGRectMake(0, … Read more

Autolayout problems with iOS8 with code that works fine on iOS7

@robmayoff has a great answer for this: https://stackoverflow.com/a/26066992/1424669 Essentially, in iOS8 you can no longer call setNeedsUpdateConstraints and setNeedsLayout on a view and expect the constraints of subviews to update. You must call these methods on the view whose constraint is changing. This is backwards compatible to iOS7. EXAMPLE: Suppose you have a ViewController with … Read more