Add Constraint to navigationBar Programmatically Swift
Asked Answered
D

5

8

I'm trying to add constraint to navigation bar, I have UIImageView, which has width, height and is centered horizontally, I want to add vertical space between UIImage and navigationBar to 0, I'm trying this for like 1 hour and couldn't figure out how, i tried adding constraint to UIView, and added constant of navbarHeight + statusBarHeight, and it worked, but I want to make relationship between imageview and navbar

let verticalSpace = NSLayoutConstraint(item: image, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1, constant: 0)
view.addConstraint(verticalSpace) // this works
Dumfries answered 29/2, 2016 at 8:16 Comment(0)
F
15

try with topLayoutGuide

let verticalSpace = NSLayoutConstraint(item: image,  
                                       attribute: .Top,  
                                       relatedBy: .Equal,  
                                       toItem: self.topLayoutGuide,  
                                       attribute: .Bottom,  
                                       multiplier: 1, constant: 0)  

The above constraint explanation:

simply its called: vertical space between image.Top & self.topLayoutGuide.Bottom = 0

that means Top constraint of image view attached with a Bottom attribute of topLayoutGuide with constant 0.

You can use anchors as well to make this possible for iOS 10+

if #available(iOS 11.0, *) {
   image.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
} else {
   image.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
}
Faceoff answered 29/2, 2016 at 8:25 Comment(5)
Yes this worked, but could you please explain differences between those two "attributes", one says .Top one says .Bottom, why is that?Dumfries
i just realized this doesn't work when using outlet UIImageView, could you tell me why?Dumfries
Let us continue this discussion in chat.Faceoff
when i create "let imageView" and add this verticalSpace, it works perfectly, but when i try to do this on @IBOutlet imageView it does not, i created imageview in storyboard so i can see whats going on and constraint doesn't work anymore, havent changed code, except instead of programatically adding imageview i addet it in storyboardDumfries
@EICaptainv2.0 may you update your answer to reflect also post iOS 10 like Andy Novak's answer says: stackoverflow.com/a/51219283Gravelblind
B
10

llkenny's answer for iOS 11.0+ : image.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true

Brinn answered 7/7, 2018 at 1:47 Comment(0)
L
3

With anchors:

image.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
Loftus answered 12/9, 2017 at 6:54 Comment(0)
P
0

In storyboard. Two constraints:

The first:

enter image description here

The second:

enter image description here

Result:

enter image description here

Prismatic answered 28/12, 2018 at 11:17 Comment(0)
C
-1

UICollectionViewExample code:

func mainCollectionViewConstraint() {
        NSLayoutConstraint.activate([
            mainCollectionView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10),
        mainCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        mainCollectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        mainCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
    ])
}
Chilpancingo answered 8/4, 2020 at 0:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.