Questions tagged [nslayoutanchor]
44 questions
40
votes
3 answers
On iOS, what are the differences between margins, edge insets, content insets, alignment rects, layout margins, anchors...?
There seem to be several different options/terms and people in the iOS community use with respect to layout (e.g. UIEdgeInsets is a type, but sometimes I hear/read "set the insets" or layout margins vs layout guides).
I've always been able to find…

kraeon
- 401
- 1
- 4
- 5
4
votes
1 answer
Is layoutMarginsGuide seems to be kind of excessive?
When i was trying to create layout constraints, i read about NSLayoutAnchor class. They said:
Note
UIView does not provide anchor properties for the layout margin
attributes. Instead, the layoutMarginsGuide property provides a
UILayoutGuide…

Ninja
- 309
- 7
- 26
4
votes
0 answers
Unable to activate constraint with anchors because they have no common ancestor
I have added a view called myView in interface builder as can be seen in the code below and have added this to the main view as a subview, I have then added three buttons programatically. I have added these as subviews to the main view before trying…

Marcus Levi
- 65
- 1
- 6
3
votes
1 answer
Add multiplier to NSLayoutAnchor constraints in Swift
Anchor constraints simplify adding constraints but the multiplier property available in storyboard does not seem to be available for all types of constraints.
For example, as per the answer here, you can center a label in a view…

user6631314
- 1,751
- 1
- 13
- 44
3
votes
1 answer
How to update anchor constraint in swift
I would like to create a menu like android in iOS. I'm using layout constraints to setup constraints. I'm facing the issue when I'm trying to update the left constraint of an image on button click (It should animate to clicked button's position).…

Damodar
- 707
- 2
- 10
- 23
3
votes
0 answers
Generics to consolidate NSLayoutAnchor
I want to consolidate the NSLayoutDimension NSLayoutXAxisAnchor and NSLayoutYAxisAnchor to create a wrapper for my anchors to avoid a long list of convenience methods
The ideal syntax I was looking for was
extension UIView {
func example() {
…

Magoo
- 2,552
- 1
- 23
- 43
3
votes
2 answers
How can I get the height of a view if I set its anchors
I am currently trying to access the height of a view which I previously set anchors for. So for example, I set the left, right, top, and bottom anchors of searchTable using the following in my…

kbunarjo
- 1,277
- 2
- 11
- 27
3
votes
2 answers
"Cannot convert value of type 'NSLayoutDimension' to expected argument type NSLayoutAnchor
I am trying to add a subview in my Swift 3 tableView. I want the leading anchor to be related to 0.05 x the width of my parent view( tableView).
When I write the following:
modalView.leadingAnchor.constraint(equalTo:margins.widthAnchor, constant:…

Thalatta
- 4,510
- 10
- 48
- 79
2
votes
0 answers
NSLayoutConstraint negative multiplier gives unexpected results
We are using Xcenter and Ycenter NSLayoutConstraint to position subview inside parent superview. As we need to keep the position proportional we can't use the constant value of the constraints to position it, it needs to be based purely on the…

Roi Mulia
- 5,626
- 11
- 54
- 105
2
votes
3 answers
heightAnchor.constraint not change height of view
I use this code:
func show(){
view.translatesAutoresizingMaskIntoConstraints = false
view.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
view.topAnchor.constraint(equalTo: view.topAnchor, constant:…

Mohammad
- 59
- 1
- 7
2
votes
3 answers
Is using NSLayoutAnchor bad?
I usually use NSLayoutAnchor but many times I have been advised to not use it. Is there any problem with it such as performance drop besides more complex/longer code?
I've been told to use:
let myView = UIView(frame: CGRect(x: 0, y: 20, width:…

Sin Sopheak
- 364
- 2
- 13
2
votes
1 answer
Console warning when using NSLayoutAnchor.constraintEqualToSystemSpacingAfter
I am using NSLayoutAnchor's constraintEqualToSystemSpacingAfter for building my layout.
NSLayoutConstraint.activate([
customView.leadingAnchor.constraintEqualToSystemSpacingAfter(safeAreaLayoutGuide.leadingAnchor, multiplier: 1)
])
It actually…

Tamás Sengel
- 55,884
- 29
- 169
- 223
2
votes
3 answers
iOS - anchor view to the top of the screen, behind status bar
I've a CollectionViewController with the default status bar and all it's embedded in a NavigationController.
I'm trying to reproduce an effect in which I have a View anchored to the top of the screen, since my navController is fully transparent and…

Ivan Cantarino
- 3,058
- 4
- 34
- 73
2
votes
2 answers
Compile error in extension NSLayoutAnchor
I am trying to create an extension method on NSLayoutAnchor to accept a multiplier. I get a weird Swift compile error (Swift 3, XCode 8.2.1) not in the extension file but in the corresponding .h file (this file is automatically generated by the…

andrewz
- 4,729
- 5
- 49
- 67
1
vote
1 answer
Subview that clips to parent view in Swift
I'm trying to add a subview that would always have the same size as its parent, so that it would always resize if the parent size changes.
I was thinking about adding (top, bottom, leading, trailing) constraints from the code.
When I add the…

Xys
- 8,486
- 2
- 38
- 56