tranvutuan
tranvutuan

Reputation: 6109

Auto Layout doesn't work as expected

Given:

a 'logo' ( uiimageview with vertical space >= 10 from top of super view) and a 'log in' and vertical spaces between logo and holder view is <=31

Goal:

in the lanscape mode, logo will move up to the top due to a change from screen'size and holder view should be moving regarding as well.

However, when I rotate from portrait to lanscape, what I am getting is that the vertical constraint still stays at 81 ( somehow i can not delete this value from xcode ). Moreover, the vertical space between a 'Main View' and 'Log in' can not be deleted as well. I know I am doing something wrong here.

Please help if you have any suggestions about this problem.

Details:

+picture 1 is the portrait mode with some details about the constraints

enter image description here

+picture 2 is the lanscape mode with some errors for it.

enter image description here

Edited : Like the way I set up I want the vertical space of logo can be shrinked but it is always >= 10 from the top of the main view

Upvotes: 1

Views: 228

Answers (2)

rdelmar
rdelmar

Reputation: 104082

I don't know if this can be done all in IB -- if so, I haven't figured it out yet. I've done it in code as I show below. I start with a constraint from the top of the image view to the top of the main view with a fixed value and also constraints between the image view and the login view. This should be enough to satisfy the system, and you can remove any other constraints to the top or bottom of the main view (I used buttons in my test which have an intrinsic height, so I didn't need to set that. If your views don't have an intrinsic or specific height set, you would have to do that also). Then, in code I remove that constraint to the top (IBOutlet conTop), and remake it to the bottom:

@implementation ViewController {
    IBOutlet NSLayoutConstraint *conTop;
    IBOutlet UIButton *button;
}

- (void)viewDidAppear:(BOOL)animated  {
    [super viewDidAppear:animated];
    [self.view removeConstraint:conTop];
    conTop = [NSLayoutConstraint constraintWithItem:button attribute:NSLayoutAttributeTop relatedBy:0 toItem:self.view attribute:NSLayoutAttributeBottom multiplier:.5 constant:-120];
    [self.view addConstraint:conTop];
    [self.view layoutSubviews];
}

By using a multiplier and a constant together, you can adjust how it moves after a rotation. The numbers I chose here looked pretty good to me, but you can mess with them to see what they do. You can think of the multiplier as a sensitivity factor -- the smaller that fraction, the less change in the distance from the top you will get on rotation. If you need exact values, you can use a little algebra to calculate the values for the multiplier and constant.

Upvotes: 1

paulmelnikow
paulmelnikow

Reputation: 17208

You need to find a way to express the relationships declaratively – and ideally in terms which work for both cases.

You haven't quite said what you want to happen in landscape mode. The logo at the top, but how far from the top? Where do you want the holder view?

There's a WWDC video which is essential for understanding how Auto Layout works. Have you had a chance to watch it yet?

Upvotes: 0

Related Questions