0
votes

I am using autolayout. As I can add constraints to view only, but in maximum case I have to use scroll bar. So if I am adding view on scrollView with CGRectmake method. Then it is not changing the dimension in landscape mode.

Snapshot in portrait mode:

enter image description here

enter image description here

Snapshot in landscape mode:

enter image description here

enter image description here

Code:

    UIScrollView *scrllView=[UIScrollView new];
    [scrllView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self.view addSubview:scrllView];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];


    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrllView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:0]];


    [scrllView setContentSize:CGSizeMake(self.view.frame.size.width, 1000)];


    UIView *SBV=[UIView new];
   [SBV setFrame:CGRectMake(0, 0, self.view.frame.size.width,1000)];
   [SBV setTranslatesAutoresizingMaskIntoConstraints:YES];
   [SBV setBackgroundColor:[UIColor redColor]];
   [scrllView addSubview:SBV];

    UIButton *BTN=[UIButton new];

    [BTN setTranslatesAutoresizingMaskIntoConstraints:NO];

    [BTN setTitle:@"UPSide" forState:UIControlStateNormal];

    [BTN setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [SBV addSubview:BTN];


    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:100]];

    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];


    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];

    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:100]];


    UIButton *BTN1=[UIButton new];
    [BTN1 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [BTN1 setTitle:@"DOWNSide" forState:UIControlStateNormal];
    [BTN1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [SBV addSubview:BTN1];


    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:100]];

    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];


    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:SBV attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];

    [SBV addConstraint:[NSLayoutConstraint constraintWithItem:BTN1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:100]];

How I can create a view that should work in landscape and porttrait mode ?

1

1 Answers

0
votes
  1. Disable size classes for use of autolayout.(From Interface Builder Document of view controller's File Inspector)
  2. Take One scrollView in ViewController's View.Take One View Inside it.
  3. Now select scrollView and select second option from Bottom bar [Pin].and select All four constraints (Leading,Top,Bottom,Trailing) and Uncheck constarint Margin Button.Add 4 Constraints.
  4. Now select View and select second Option from Bottom bar [Pin] and select All four constraint (Leading,Top,Bottom,Trailing) and Uncheck constarint Margin Button. Select Height Constraint too(if height>568 than set your bottom Constraint 0). Now Add 5 constraints. And select first Option from Bottom bar [Align].And select second last option Horizonatal center in container and add 1 constraint.