gpt4 book ai didi

ios - 在UIScrollView中使用代码自动布局

转载 作者:行者123 更新时间:2023-12-01 17:58:12 24 4
gpt4 key购买 nike

我正在尝试使用来自代码的自动布局来正确处理UIScrollView中的旋转,该UIScrollView包含在运行时为UIPageControl的每个页面生成的许多UILabel。

我没有找到太多示例,所以我问是否有人使用NSLayoutConstraints自动布局的UIScrollView的示例。

编辑过
更清楚地说:我在运行时创建了不同的UILabel并将它们添加到UIScrollView中。在肖像中是:


(来源:disclafani.org)

在风景中是错误的:

(来源:disclafani.org)

如何使用自动布局解决?我试过使用:

NSLayoutConstraint *cons1=[NSLayoutConstraint
constraintWithItem:label
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:label2 attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:[UIScreen mainScreen].bounds.size.width*-1
];

但这是行不通的,我不认为这是正确的方法

最佳答案

实际上,有两种截然不同的策略,Apple在this article中进行了详细描述,并在这两个示例中进行了说明:

  • https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/iOS6bookExamples/ch20p573scrollViewAutoLayout
  • https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/iOS6bookExamples/ch20p573scrollViewAutoLayout2

  • 但是请记住,UILabel是一个非常特殊的情况,因为它使用了内在的大小规则。在不了解案件详细信息的情况下很难说更多。

    关于ios - 在UIScrollView中使用代码自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13887445/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com