0

enter image description hereI have added my own custom uiview to self.view. As we can't set constraints in storyboard in this scenario, i tried to add programatically.

Followed this How to Create layout constraints programmatically.

I am using below code.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    [customView setNeedsUpdateConstraints];
}

-(void)updateViewConstraints
{
    UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[self.view addConstraints:@[

                            //view1 constraints
                            [NSLayoutConstraint constraintWithItem:customView
                                                         attribute:NSLayoutAttributeTop
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeTop
                                                        multiplier:1.0
                                                          constant:padding.top],

                            [NSLayoutConstraint constraintWithItem:customView
                                                         attribute:NSLayoutAttributeLeft
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeLeft
                                                        multiplier:1.0
                                                          constant:padding.left],

                            [NSLayoutConstraint constraintWithItem:customView
                                                         attribute:NSLayoutAttributeBottom
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeBottom
                                                        multiplier:1.0
                                                          constant:-padding.bottom],

                            [NSLayoutConstraint constraintWithItem:customView
                                                         attribute:NSLayoutAttributeRight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.view
                                                         attribute:NSLayoutAttributeRight
                                                        multiplier:1
                                                          constant:-padding.right],

                            ]];


}

I am calling [self updateViewConstraints] from viewDidLoad,but still i am getting half view in landscape.

Any idea on this. Thanks in Advance.

Community
  • 1
  • 1
Steve Gear
  • 757
  • 2
  • 16
  • 44

3 Answers3

0

You can use Masonry for this purposes. As example:

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
make.left.equalTo(superview.mas_left).with.offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(superview.mas_right).with.offset(-padding.right);}];

This will set top, right, bottom and left constraints for your view.

ko7tya
  • 1
  • 1
  • thanks for the reply. I have followed as you suggest but still i am getting half screen after rotate. I have added this code in viewDidLoad. Do i need to add any? – Steve Gear Sep 19 '16 at 10:50
  • @SteveGear try to add it in viewDidAppear, because in viewDidLoad views superview is not setuped – ko7tya Sep 19 '16 at 11:15
0

try in willRotateToInterfaceOrientation:duration:call setNeedsUpdateConstraints to view that needs its constraints updated.

Unless view is not notified it wont update constraints

aman.sood
  • 872
  • 7
  • 19
0

I your constraints are set correctly, the view will resize according to them when the user changes the orientation. If you show us the your code, it would be easier for us to help you.

Some hints:

  • Be sure to set the translatesAutoresizingMaskIntoConstraints property to NO
  • You can use the visual format to set your constraints, it is sometimes easier not to forget one : Visual Format Language
  • Check your logs to see if a constraint is not broken

If your constraints are set correctly, the view should be resized according to its superview frame.

UPDATE

I see two possibilities: either the constraints are not really set (because the view is not prepared for instance), either one constraint or more are broken (but you should see some logs in that case).

I give you some code that works for me (I personally use Visual Format for constraints when possible) :

customView = [UIView new];
customView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:customView];

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

NSDictionary *views = @{ @"customView" : customView };
NSDictionary *metrics = @{ @"top" : @(padding.top) , @"bottom" : @(padding.bottom) , @"right" : @(padding.right) , @"left" : @(padding.left) };
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[customView]-right-|" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[customView]-bottom-|" options:0 metrics:nil views:views]];

I don't use the method [customView setNeedsUpdateConstraints]; when a rotation occurred : the system updates the constraints of the subviews by itself.

  • Another possible raisons : be sure your view has been added before your set the constraints. What is more, the documentation tell us to use the method `[NSLayoutConstraint activateConstraints:]`instead of `[view addConstraints:]`: [Apple doc](https://developer.apple.com/reference/uikit/uiview/1622513-addconstraints?language=objc) – Olivier Stepien Sep 19 '16 at 12:15
  • both are same @Olivier – Steve Gear Sep 19 '16 at 12:20
  • thanks for the code. But still i am getting half screen after rotation.:( – Steve Gear Sep 21 '16 at 09:12
  • i uploaded image which is getting after rotate..used same code as you suggested. – Steve Gear Sep 21 '16 at 09:26
  • That's weird. You should have some code somewhere which breaks the constraints? Don't you have any logs? Do you use a storyboard to instantiate your controller? Without seeing all your code, it is hard to tell you where the problem is. – Olivier Stepien Sep 27 '16 at 11:31
  • Yes. I am using story board to instantiate view controller and adding custom uiview to that. I am not getting any logs, but view is not fitting in landscape. I am using the same code which you suggested – Steve Gear Oct 04 '16 at 08:07
  • Make sure you have enabled Auto Layout in the storyboard file. I you have added additional views in the storyboard, be sure that all the constraints have been set too. – Olivier Stepien Oct 06 '16 at 14:48