Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Long story short, I want to position my UILabel so that it's, say, 150pt from the top of the screen. This allows it to be a good distance from the top on both a 4" iOS device or a 3.5" iOS device.

However, as soon as I use [self.navigationController setNavigationBarHidden:YES animated:NO]; the navigation bar hides which causes the view to resize and get taller. This causes my UILabel to jump upward, which I don't want it to do. It's when the user wants to go into a full screen view, and in this case things shouldn't be jumping around.

Now you may say, make the constraint from the bottom of the view. That works in theory, but it causes the UILabel to be too far down the screen on an iPhone 5. I could hardcode the value, but that doesn't quite seem to be in the spirit of Auto Layout.

Basically, a perfect solution would be to be able to set the constraint to the top of the screen not the view. Is something like this possible? If not, how would I best do this?

share|improve this question
add comment (requires an account with 50 reputation)

3 Answers

Why not use the navigation bar height (self.navigationController.navigationBar.frame.size.height), and move the UILabel up/down depending on whether the navigation bar is hidden / visible?

This way, you're not hardcoding anything and you're working off of the UINavigationBar properties.

share|improve this answer
add comment (requires an account with 50 reputation)

AutoLayout deals with Views not screens unfortunately. Here are some solutions:

1: You could say, reposition it yourself whenever layoutsubviews (or a similar trigger is performed).

2: You could also set the height to be variable so that it takes up slack when available.

3: Add a view that anchors from the bottom and fills up to the top (you can adjust the view to stay the same size or change, and add this label with a constrain to be at the top of this view).

share|improve this answer
add comment (requires an account with 50 reputation)

Make an IBOutlet to the top constraint (I call it topCon in my example) and either add or subtract the height of the bar from the constraint's constant when you hide or unhide it.

- (IBAction)hideNavBar:(UIButton *)sender {
    self.navigationController.navigationBarHidden = !self.navigationController.navigationBarHidden;
    if (self.navigationController.navigationBarHidden) {
        self.topCon.constant = self.topCon.constant + self.navigationController.navigationBar.frame.size.height;
    }else{
      self.topCon.constant = self.topCon.constant - self.navigationController.navigationBar.frame.size.height;
    }
}

This will work correctly in both portrait and landscape modes where the navigation bar has different heights.

share|improve this answer
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.