我可以使用自动布局为横向和纵向方向提供不同的约束吗?

2024-01-05

当设备旋转时是否可以改变约束?如何实现这一目标?

一个简单的例子可能是两张图像,在纵向时,一张放在另一张上面,但在横向时,它们并排。

如果这是不可能的,我还能如何完成这个布局?

我正在代码中构建我的视图和约束,而不是使用界面生成器。


Edit:运用新概念尺码等级 https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/AboutAdaptiveSizeDesign.html在 Xcode 6 中引入,您可以轻松地在 Interface Builder 中为特定尺寸类设置不同的约束。大多数设备(例如所有当前的 iPhone)都有一个Compact横向模式下的垂直尺寸类别。

对于总体布局决策来说,这是一个比确定设备方向更好的概念。

话虽这么说,如果你真的需要知道方向,UIDevice.currentDevice().orientation是要走的路。


原帖:

覆盖updateViewConstraints的方法UIViewController为特定情况提供布局约束。这样,布局始终根据情况设置正确的方式。确保它们与故事板中创建的约束形成一套完整的约束。您可以使用 IB 来设置一般约束,并将那些需要更改的内容标记为在运行时删除。

我使用以下实现为每个方向提供一组不同的约束:

-(void)updateViewConstraints {
    [super updateViewConstraints];

    // constraints for portrait orientation
    // use a property to change a constraint's constant and/or create constraints programmatically, e.g.:
    if (!self.layoutConstraintsPortrait) {
        UIView *image1 = self.image1;
        UIView *image2 = self.image2;
        self.layoutConstraintsPortrait = [[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[image1]-[image2]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(image1, image2)] mutableCopy];
        [self.layoutConstraintsPortrait addObject:[NSLayoutConstraint constraintWithItem:image1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem: image1.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
        [self.layoutConstraintsPortrait addObject:[NSLayoutConstraint constraintWithItem:image2 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:image2.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    }

    // constraints for landscape orientation
    // make sure they don't conflict with and complement the existing constraints
    if (!self.layoutConstraintsLandscape) {
        UIView *image1 = self.image1;
        UIView *image2 = self.image2;
        self.layoutConstraintsLandscape = [[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[image1]-[image2]-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(image1, image2)] mutableCopy];
        [self.layoutConstraintsLandscape addObject:[NSLayoutConstraint constraintWithItem:image1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:image1.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
        [self.layoutConstraintsLandscape addObject:[NSLayoutConstraint constraintWithItem:image2 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem: image2.superview attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    }

    BOOL isPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
    [self.view removeConstraints:isPortrait ? self.layoutConstraintsLandscape : self.layoutConstraintsPortrait];
    [self.view addConstraints:isPortrait ? self.layoutConstraintsPortrait : self.layoutConstraintsLandscape];        
}

现在,您所需要做的就是每当情况发生变化时触发约束更新。覆盖willAnimateRotationToInterfaceOrientation:duration:对方向变化的约束更新进行动画处理:

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

    [self.view setNeedsUpdateConstraints];

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我可以使用自动布局为横向和纵向方向提供不同的约束吗? 的相关文章

  • 如何在代码中编辑约束

    我有一个以 100 开始宽度限制的网页 当用户单击按钮时 我想将约束更改为 200 我试过这个 NSLayoutConstraint constrain NSLayoutConstraint constraintWithItem self
  • CoreBluetooth:检测设备超出范围/连接超时

    我正在设计一个 iOS 框架来处理多个 BLE 设备 均为同一类型 目前一切都运行良好 除了一件事 客户想要一个包含可用设备的列表 但是 我如何检测过去发现的设备何时不再可用 当我尝试连接到不再可用的设备时 会出现另一个问题 文档说 连接尝
  • 在 SWIFT 中发送本地通知

    我将此代码添加到我的第一个 ViewController 中 Step 1 Ask permission from User let center UNUserNotificationCenter current center reques
  • React Native facebook iOS sdk 构建失败

    我已遵循 Facebook 开发人员指南中列出的 iOS React Native sdk 的所有准则 但我仍然无法构建该应用程序 附上我的配置和构建日志的屏幕截图 Ld Users alaaattya Library Developer
  • CAShapeLayer 上的渐变颜色效果

    我正在尝试在 CAShapeLayer 上应用渐变颜色 为此我编写代码 void addCircle Drawing code UIBezierPath aPath UIBezierPath bezierPathWithArcCenter
  • 修改 SCNParticleEventBlock 中的 SCNParticleSystem 颜色不起作用

    鉴于提供的示例代码handle forProperties handler https developer apple com documentation scenekit scnparticlesystem 1523251 handle
  • iOS 9.3 中的 KVO 被破坏

    这可能是 iOS 9 3 发行版 中的一个可怕的错误 添加单个观察者时 NSUserDefaults standardUserDefaults 我注意到响应方法 observeValueForKeyPath ofObject change
  • iOS 和 Firebase 自动续订订阅

    我的问题 我很难找到一种使用 Firebase 在 iOS 中安全管理自动续订订阅的方法 购买流程 User1 purchases a subscription 使用订阅标识符更新 Firebase 上 User1 的帐户 用于解锁内容 存
  • 寻找可靠的方法来选择iPhone 13微距相机设备

    我正在使用 objc 开发一个应用程序 我必须选择 iPhone 13 Pro 和 13 Pro Max 中配备的新微距摄像头 目前 我在所有其他设备上选择我的相机 并剪掉此代码 AVCaptureDeviceDiscoverySessio
  • 在phonegap中播放本地声音

    我有一个 wav文件在我的www文件夹 我正在使用 jQuery 和以下代码 警报响起 但声音不播放 难道我做错了什么
  • 在 iOS 上构建 WebRtc

    我按照以下说明成功地在 MAC 上构建了 WebRTC http www webrtc org reference getting started http www webrtc org reference getting started
  • CSS 安全区域属性在 iPhone X 上不起作用

    就我而言 我正在 iPhone X 上运行一个 Web 应用程序 我尝试在顶部添加一个填充 以使用 Webkit 的安全区域 css 属性将我的身体推到安全区域padding top constant safe area inset top
  • 如何在 Alamofire 中使用“responseDecodable”方法?

    I have been trying to use responseDecodable method from Alamofire but I m getting Generic parameter T could not be infer
  • 如何获得 UICollectionView 的矩形

    我想在 UICollectionView 中找到节标题的框架 我对 UITableView 也有类似的情况 为此 我能够通过执行以下操作来获得其正确性 CGRect rect self tableView rectForHeaderInSe
  • 获取动画滚动视图的当前位置

    我遇到了以下问题 我对 uiscrollview 进行了子类化 其中 contentOffset 通过以下代码进行动画处理 UIView animateWithDuration 1 0 delay 1 0 options options a
  • XOR或其他简单的文件混淆方法ios目标c

    我正在开发一个使用受版权保护的 pdf 文件的 ios 应用程序 我正在寻找一些简单的方法来混淆文件以确保安全 这些方法不需要我经历额外的大众市场 CCATS 加密审查流程 但也将确保版权所有者已尽我所能来保证他们的数据安全 当然 我用密码
  • 在 Swift 中计算两个 CLLocation 点之间的方位角 [重复]

    这个问题在这里已经有答案了 我正在尝试计算仅 swift 代码中两个 CLLocation 点之间的方位 我遇到了一些困难 并假设这是一个非常简单的函数 堆栈溢出似乎没有列出任何内容 func d2r degrees Double gt D
  • 如何动态添加XCTestCase

    我正在为一个白标签项目编写 UI 测试 其中每个应用程序都有一组不同的菜单项 测试点击每个菜单项并截取屏幕截图 使用快车道快照 https docs fastlane tools actions snapshot 目前这一切都发生在一个内部
  • iOS 上的推送通知渐进式 Web 应用程序

    我需要开发一个集成了推送通知的渐进式网络应用程序 在网上搜索我发现了关于这个主题的不同意见 如果我理解正确的话 目前我们无法在移动版 safari 中推送通知 但仅限桌面版 这样对吗 你有什么建议来获得相同的结果吗 我不是iOS专家 我想知
  • 如何从 App Store Connect 中删除自动创建的 macOS 应用程序

    I have an iOS app Recently macOS app automatically appeared in App Store Connect 我不打算在 macOS 上发布 iOS 应用程序 我怎样才能摆脱它 我打开了

随机推荐