简体   繁体   中英

How to change UIView constraints based on device orientation using Objective C?

I am trying to create iOS storyboard with multiple view for portrait and landscape . Everything I have created storyboard constraints and autolayout .

Now my problem is for portrait I need to show like below portrait Image. Thats I have done by using storyboard constraints.

For landscape Need to show First View (red) and bottom bar only other I have hidden by hardcode. But I don't know how to expand the red screen first view full height upto bottom bar top.

NOTE : This is Universal App.

- (void) orientationChanged:(NSNotification *)note
{
    UIDevice * device = note.object;
    switch(device.orientation)
    {
        case UIDeviceOrientationPortrait:
            /* start special animation */
            NSLog(@"Portrait");
            self.namelist_tableview.hidden = NO;// ORANGE VIEW
            break;

        case UIDeviceOrientationLandscapeRight:
            /* start special animation */
            NSLog(@"Landscape");
            self.namelist_tableview.hidden = YES;// ORANGE VIEW
            break;

        case UIDeviceOrientationLandscapeLeft:
            /* start special animation */
            NSLog(@"Landscape");
            self.namelist_tableview.hidden = YES;// ORANGE VIEW
            break;

        default:
            break;
    };
}

在此处输入图片说明

you can add the red view height constraint, store it in your UIViewController . when the device's orientation changed. if portrait, set the ret view height constraint equal how long you want to set, and landscape set the red view height constraint your initial value.

It can be done easily if you use size classes.

Use cmd+delete for the views which you don't want in landscape mode.and set others accordingly.

显示图像1

显示图像2

Very simple, add constrains in intefacebuilder for portrait mode , add constrains for landscape mode. Next make an outlet for all constrains. In device change orientation event do this

if you are in landscape mode

+Set active constrains for portrait to false (constraint.active = NO)

+Set active of constrains for landscape to true (constraint.active = YES)

if you are in portrait mode

+Set active of constrains for landscape to false (constraint.active = NO)

+Set active of constrains for portrait to true (constraint.active = YES)

If you did not fix the height of the red view than you can do something like this:

Create a instance of height constraint of the yellow color view like,

Step 1: Select height constraint as I did 在此处输入图片说明

-

Step 2: Make a instance in you view controller interface 在此处输入图片说明

After that write below line of code to your orientationChanged: method self.heightConstarintYelloView.constant = 0.f;

It will set you yellow box height to zero and because of the vertical spacing between red view and yellow view, red view will than expand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM