It is necessary with the help of NSLayoutConstraint configure the placement of the testView at a fixed distance under the UINavigationBar . The problem is that the height of the UINavigationBar changes as the screen rotates.

How to set up the restrictions for changing the height of the UINavigationBar ?

see block "// Y" in the example code

 self.testView = [[UIView alloc] initWithFrame:CGRectZero]; [self.testView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addSubview:self.testView]; // Width [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.testView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:100]]; // Height [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.testView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]]; // Y [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.testView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:70.f]]; // X [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.testView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]]; 

The result now looks like this:

horizontally vertically

    1 answer 1

    You need to look at the topLayoutGuide and use not the Top attribute, but the Bottom .

    It is also recommended to use the new API - NSLayoutAnchor

      let redView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 50))) redView.backgroundColor = .redColor() redView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(redView) redView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor).active = true redView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 16).active = true redView.heightAnchor.constraintEqualToConstant(50).active = true redView.widthAnchor.constraintEqualToConstant(100).active = true 
    • The code sample was under swift , but the advice helped, thanks. attribute:NSLayoutAttributeBottom - Alexey Alybin