OGeek|极客世界-中国程序员成长平台

标题: ios - 如何使用 Objective C 根据设备方向更改 UIView 约束? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:57
标题: ios - 如何使用 Objective C 根据设备方向更改 UIView 约束?

我正在尝试为 portraitlandscape 创建具有多个 View 的 iOS storyboard。我创建的所有 Storyboard constraintsautolayout

现在我的问题是肖像我需要显示如下肖像图像。这就是我通过使用 Storyboard 约束完成的。

对于横向需要显示 First View (red) 和底部栏只有其他我已通过硬编码隐藏。但是我不知道如何将红屏首先查看全高扩展到底栏顶部。

注意:这是通用应用程序。

- (void) orientationChangedNSNotification *)note
{
    UIDevice * device = note.object;
    switch(device.orientation)
    {
        case UIDeviceOrientationPortrait:
            /* start special animation */
            NSLog(@"ortrait");
            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;
    };
}

enter image description here



Best Answer-推荐答案


很简单,在interfacebuilder中为纵向模式添加约束, 为横向模式添加约束。 接下来为所有约束创建一个导出。 在设备更改方向事件中执行此操作

如果您处于横向模式

+将纵向的事件约束设置为 false (constraint.active = NO)

+将横向约束的事件设置为 true (constraint.active = YES)

如果您处于纵向模式

+将横向约束的事件设置为 false (constraint.active = NO)

+将肖像的约束设置为true(constraint.active = YES)

关于ios - 如何使用 Objective C 根据设备方向更改 UIView 约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202300/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4