2,150 questions
573
votes
19
answers
706k
views
How to add constraints programmatically using Swift
I'm trying to figure this out since last week without going any step further. Ok, so I need to apply some constraints programmatically in Swift to a UIView using this code:
var new_view:UIView! = ...
252
votes
3
answers
108k
views
setNeedsLayout vs. setNeedsUpdateConstraints and layoutIfNeeded vs updateConstraintsIfNeeded
I know that the auto layout chain consists in basically 3 different process.
updating constraints
layout views (here is where we get calculation of frames)
display
What's is not totally clear to me is ...
187
votes
17
answers
119k
views
UITextView that expands to text using auto layout
I have a view that is laid out completely using auto layout programmatically. I have a UITextView in the middle of the view with items above and below it. Everything works fine, but I want to be able ...
183
votes
19
answers
107k
views
Can I change multiplier property for NSLayoutConstraint?
I created two views in one superview, and then added constraints between views:
_indicatorConstrainWidth = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeWidth relatedBy:...
161
votes
17
answers
159k
views
Unable to simultaneously satisfy constraints, will attempt to recover by breaking constraint
Below is the error message I receive in the debug area. It runs fine and nothing is wrong except that I receive this error. Would this prevent apple accepting the app? How do I fix it?
2012-07-26 01:...
143
votes
10
answers
238k
views
How to update the constant height constraint of a UIView programmatically?
I have a UIView and I set the constraints using Xcode Interface Builder.
Now I need to update that UIView instance's height constant programmatically.
There is a function that goes like myUIView....
132
votes
6
answers
26k
views
What happens with constraints when a view is removed
The question I have is simple but I couldn't find any information in the documentation.
What happens with layout constraints when a view is removed from the view hierarchy (or moved to another view)?
...
129
votes
6
answers
85k
views
iOS: How does one animate to new autolayout constraint (height)
I've never worked with autolayout constraints before. I have a small new app I'm working on and noticed that the NIB's views are defaulting to autolayout. So, I figured I'd take the opportunity to ...
128
votes
18
answers
173k
views
How to adjust height of UICollectionView to be the height of the content size of the UICollectionView?
I would like the UICollectionView (The red one) to shrink to the height of the content size in this case UICollectionViewCells(the yellow ones) because there is a lot of empty space. What I tried is ...
120
votes
8
answers
106k
views
When can I activate/deactivate layout constraints?
I've set up multiple sets of constraints in IB, and I'd like to programmatically toggle between them depending on some state. There's a constraintsA outlet collection all of which are marked as ...
115
votes
7
answers
49k
views
Autolayout: Add constraint to superview and not Top Layout Guide?
I have a UIView in my UIViewController in storyboard which I want to add a constraint on to space that view a distance from the top edge.
Now, when I do the usual by ctrl + drag to the ViewController'...
109
votes
7
answers
141k
views
Programmatically Add CenterX/CenterY Constraints
I have a UITableViewController that doesn't display any sections if there is nothing to show. I've added a label to indicate to the user that there is nothing to display with this code:
label = ...
108
votes
7
answers
132k
views
How can I set aspect ratio constraints programmatically in iOS? [duplicate]
I have used auto layout for my view controllers. I have set the V and H positions in constraints, but I want to know how can I increase my button size when it changes to 5s, 6 and 6 Plus. This is the ...
106
votes
10
answers
64k
views
Auto layout constraints issue on iOS7 in UITableViewCell
I'm using auto layout constraints programmatically to layout my custom UITableView cells and I'm correctly defining the cell sizes in tableView:heightForRowAtIndexPath:
It's working just fine on iOS6 ...
105
votes
1
answer
32k
views
NSLayoutConstraint crashes ViewController [duplicate]
Possible Duplicate:
presentViewController: crash on iOS 6 (AutoLayout)
I'm getting this error when clicking on a button in my app:
2012-06-28 21:43:36.860 AppName[2403:707] *** Terminating app ...
95
votes
4
answers
51k
views
"Width equals height" constraint in Interface Builder
I can't find a way to create a 'square' constraint, meaning 'width equals height' in Interface Builder. I guess it's possible to add such constraint programmatically. Is there something I can do in IB?...
86
votes
11
answers
96k
views
Width and Height Equal to its superView using autolayout programmatically?
I've been looking for a lot of snippets in the net and I still can't find the answer to my problem. My question is I have a scrollView(SV) and I want to add a button inside scrollView(SV) ...
83
votes
8
answers
54k
views
Centering subview's X in autolayout throws "not prepared for the constraint"
I have a custom UIView subclass which is being initialized via a nib.
In -awakeFromNib, I'm creating a subview and attempting to center it in its superview.
[self setInteralView: [[UIView alloc] ...
77
votes
11
answers
115k
views
Unable to simultaneously satisfy constraints - No constraints in place
I have gone through and removed every single user constraint yet I am still getting the following error ONLY after I rotate the device. I have absolutely no clue why though. Does anyone have any ideas?...
77
votes
3
answers
57k
views
When should translatesAutoresizingMaskIntoConstraints be set to true?
I've read the documentation. But I'm still not sure when I need to set it to true. In the code below if I set it to false I won't see the header at all. If I leave it as true, then everything is fine.
...
73
votes
8
answers
79k
views
With Auto Layout, how do I make a UIImageView's size dynamic depending on the image?
I want my UIImageView to grow or shrink depending on the size of what the actual image it's displaying is. But I want it to stay vertically centered and 10pts from the leading edge of the superview.
...
68
votes
13
answers
14k
views
What is _UITemporaryLayoutWidth and why does it break my constraints?
I have a storyboard with Autolayout and Size Classes. Pretty complicated layout, and unfortunately I can't really pin down how to reproduce the problem in a new project.
But the view in question is ...
65
votes
5
answers
38k
views
iOS 7: Misplaced View Frame for "Label - Label" will be different at run time
I just finished an app on iOS 6 and bought a developer account a week ago so haven't had much time playing with the iOS 7 SDK. Just downloaded the Golden Master version and trying to upgrade my app to ...
65
votes
4
answers
28k
views
Creating Auto Layout constraints to topLayoutGuide and bottomLayoutGuide in code
Apple's documentation on creating Auto Layout constraints between a view and one of the layout guides only shows an example using VFL.
Is there any way to create these constraints programmatically ...
63
votes
5
answers
83k
views
Update height constraint programmatically
I am new in auto layout. I have done all of my project from xib file, but now I faced a problem where I have to update an view's height programmatically. I have tried below but now working.
[[self ...
62
votes
5
answers
44k
views
UITableView within UIScrollView using autolayout
At the moment, I'm using a UITableView along with other views that are contained in a UIScrollView. I want the UITableView to have its height to be the same as its content height.
To complicate ...
59
votes
6
answers
16k
views
Animate intrinsicContentSize changes
I have a UIView subclass that draws a circle whose radius changes (with nice bouncy animations). The view is deciding the size of the circle.
I want this UIView subclass to change its frame size to ...
57
votes
9
answers
62k
views
UICollectionView autosize height
How do I properly resize a UICollectionView so that it fully displays its contents? I have tried many things, including setting its frame, calling reloadData and invalidating the layout:
self....
55
votes
3
answers
35k
views
What's with Constraints in SwiftUI?
What is happening now with constraints in SwiftUI? Do View types adapt automatically for bigger devices etc. or what should we have to do instead?
53
votes
7
answers
17k
views
What is a 'UIView-Encapsulated-Layout-Width' constraint?
I keep getting 'Unable to simultaneously satisfy constraints' exceptions (Xcode 5, iOS 7, both device and simulator), where one of the constraints in the list is something like this:
"<...
49
votes
9
answers
61k
views
AutoLayout to keep view sizes proportional
I'm trying to achieve the following:
I have 2 views in my xib that need to stay 20 pixels off the edge (both sides and top)
The 2 views that need to resize aren't the same size
They have to be 20 ...
48
votes
3
answers
48k
views
iOS change auto layout constraints when device rotates
I want to modify the layout constraints when the device rotates.
My UIViewController is composed of 2 UIViews, in landscape they are horizontally aligned,
and in portrait they are vertically aligned.
...
45
votes
8
answers
54k
views
Set UITableView's height to the height of its content with Auto Layout
I have a View which has two labels and a Table View inside it. I want label 1 to always stay above my Table View and label 2, to be below the Table View. The problem is that the Table View needs to ...
38
votes
1
answer
52k
views
Update the constant property of a constraint programmatically in Swift?
I want to animate an object, so I declare a constraint and add it to the view. I then update the constant property of the constraint inside an UIView animation. Why doesn't this code move the object?
...
36
votes
3
answers
23k
views
Enable + Disable Auto-Layout Constraints
I have a simple (I think) problem: I have a UIImageView, which I have set multiple constraints for in the storyboard.
At times, I'll need to disable the constraints, and set its frame manually, but ...
36
votes
4
answers
25k
views
UITextView inside UIScrollView with AutoLayout
I am trying to place UITextView inside UIScrollView with AutoLayout with no luck. What I have tried is,
I placed UIScrollView inside the main view in Storyboard
I placed UITextView inside UIScrollView ...
33
votes
5
answers
59k
views
NSLayoutConstraints and setting the width/height of a view dynamically
I have a question about setting the size of a view to which i'm applying some layout constraints.
How can I define the size of the view without defining its frame?
I'm trying to create a view that ...
33
votes
4
answers
26k
views
Hiding a UIView using AutoLayout constraints
From time to time I have a subview that I would like to remove from a layout. Not only should it be hidden, but it should not be considered part of the view's 'flow', so to speak. An example:
I am ...
32
votes
6
answers
27k
views
Building a titleView programmatically with constraints (or generally constructing a view with constraints)
I'm trying to build a titleView with constraints that looks like this:
I know how I would do this with frames. I would calculate the width of the text, the width of the image, create a view with that ...
32
votes
3
answers
28k
views
Understanding multiplier in auto layout to use relative positioning
I am trying to understand how one can utilize Auto Layout to position items relative to other views percentage-wise.
For example, I recently learned that you can specify a view's bottom should lie 4% ...
31
votes
2
answers
3k
views
How can I rearrange views when autorotating with autolayout?
I frequently run into this problem where I want my views arranged in one manner for portrait and a somewhat drastically different manner for landscape.
For a simplified example, consider the ...
29
votes
3
answers
12k
views
Handling AutoLayout constraint animation differences in iOS 10?
I've noticed that in iOS 10 Beta 5 (about to try Beta 6), AutoLayout constraint animation behaves a bit differently.
For example, this approach does not work the same as it did in previous iOS ...
29
votes
3
answers
64k
views
How to set UILabel only width and height and constraints programmatically
I want to create a UILabel programmatically with height, width and then I want to add constraints to it also programmatically for positioning the UILabel.
Update:
I want to create UI like this:
How ...
29
votes
5
answers
30k
views
How to add Equal Spacing and equal width for button in iOS Auto layout
Am new to Auto layout constraints in Xcode. I have one Bottom View like UITabBar with 6 UIButtons. Without constraints I aligned those buttons with 5 space padding with each buttons and each buttons ...
28
votes
3
answers
10k
views
activateConstraints: and deactivateConstraints: not persisting after rotation for constraints created in IB
The new NSLayoutConstraint methods activateConstraints: and deactivateConstraints: don't appear to work correctly with IB-created constraints (they do work correctly for code-created constraints). I ...
28
votes
1
answer
10k
views
What is the difference between all these Auto Layout update methods? Are all necessary?
In the code below, these four methods are called for layout reasoning. I'm a little confused why all of them are needed, though, and what they do differently from one another. They're used in the ...
27
votes
1
answer
14k
views
Install/Uninstall constraint programmatically [closed]
Is there a way to install and uninstall an Xcode interface builder constraint programmatically?
26
votes
4
answers
76k
views
Setting constraints programmatically
I'm experimenting with how to use UIScrollView. After much trouble, I finally got the hang of it. But now I've seem to hit another snag.
In this simple app, I have a scroll view with and in order for ...
26
votes
3
answers
11k
views
How do you make Interface Builder respect a custom view's intrinsic content size in constraint based layout?
Interface Builder in XCode 4.5 respects the intrinsicContentSize for some views, e.g. NSButton, but I can't convince it to respect it on my own custom subviews. This causes IB to add extra constraints ...
25
votes
5
answers
23k
views
Unable to make outlet connection to a constraint in IB
I am running XCode 5 with iOS 7. I need to dynamically change the height constraint of a UITextView prior to its layout. I have seen many postings stating that this can be done by making an outlet ...