layoutSubViews后视图框架宽度不正确,违反了对iOS 9

时间:2015-09-15 03:27:27

标签: ios autolayout

我正在使用Xcode 7在官方iOS 9上测试我的应用程序。

我可以通过一个简单的演示项目重现它:

https://github.com/liuxuan30/autolayoutProblemOniOS9

我的应用在iOS 7和iOS上运行良好8,但是当我在iOS 9上运行时,UILabel的一些框架宽度与其约束条件不一致:

这就是我设置布局的方式,非常简单,UICollectionViewCell.xib中有两个标签,并将前导和尾随对齐为单元格,固定高度和上边距。

热门标签:

enter image description here

底部标签:

enter image description here

但是,在运行时,

当我在UICollectionViewCell的layoutSubViews中添加断点时:

-(void)layoutSubviews {
    [super layoutSubviews]; // break here
}

调用[super layoutSubViews]后,topLabel的框架与其约束条件不同:

// before [super layoutSubViews]
(lldb) po self.topLabel
<UILabel: 0x7fc636ff6450; frame = (0 15; 90 21); text = '本周市场份额'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fc636ff6650>>

(lldb) po self.topLabel.constraints
<__NSArrayI 0x7fc6371cf9e0>(
<NSContentSizeLayoutConstraint:0x7fc637300960 H:[UILabel:0x7fc636ff6450'本周市场份额'(90)] Hug:250 CompressionResistance:750>,
<NSContentSizeLayoutConstraint:0x7fc637301d80 V:[UILabel:0x7fc636ff6450'本周市场份额'(18)] Hug:250 CompressionResistance:750>,
<NSLayoutConstraint:0x7fc636ff6d10 V:[UILabel:0x7fc636ff6450'本周市场份额'(18)]>
)

// after [super layoutSubViews]
(lldb) po self.topLabel
<UILabel: 0x7fc636ff6450; frame = (0 15; 64 18); text = '本周市场份额'; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7fc636ff6650>>

宽度错误,它应该是90,但实际上在[super layoutSubViews]之后是64。它只发生在iOS 9上,这让我很困惑。请帮忙!

编辑:这是UICollectionViewCell在[super layoutSubViews]之前/之后的帧信息和约束:

// before [super layoutSubViews]
(lldb) po self
<BDPDetailCollectionViewCell: 0x7fbe55505a50; baseClass = UICollectionViewCell; frame = (0 0; 90 64); clipsToBounds = YES; layer = <CALayer: 0x7fbe55505eb0>>

(lldb) po self.constraints
<__NSArrayI 0x7fbe55458940>(
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cd30 h=--& v=--& UIView:0x7fbe555060e0.midX == + 45>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cd80 h=--& v=--& H:[UIView:0x7fbe555060e0(90)]>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cdd0 h=--& v=--& UIView:0x7fbe555060e0.midY == + 32>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550b850 h=--& v=--& V:[UIView:0x7fbe555060e0(64)]>,
<NSLayoutConstraint:0x7fbe555080d0 UIView:0x7fbe555060e0.trailing == UILabel:0x7fbe55506dd0'17.06'.trailing>,
<NSLayoutConstraint:0x7fbe55508120 UILabel:0x7fbe55506dd0'17.06'.leading == UIView:0x7fbe555060e0.leading>,
<NSLayoutConstraint:0x7fbe55508170 UILabel:0x7fbe55506dd0'17.06'.top == UIView:0x7fbe555060e0.top + 38>,
<NSLayoutConstraint:0x7fbe555081c0 UILabel:0x7fbe55507a40'本周市场份额'.leading == UIView:0x7fbe555060e0.leading>,
<NSLayoutConstraint:0x7fbe55508210 UIView:0x7fbe555060e0.trailing == UILabel:0x7fbe55507a40'本周市场份额'.trailing>,
<NSLayoutConstraint:0x7fbe55508260 UILabel:0x7fbe55507a40'本周市场份额'.top == UIView:0x7fbe555060e0.top + 15>,
<NSLayoutConstraint:0x7fbe5550c6e0 'UIView-Encapsulated-Layout-Height' V:[BDPDetailCollectionViewCell:0x7fbe55505a50(64)]>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cad0 h=--& v=--& 'UIView-Encapsulated-Layout-Left' H:|-(0)-[BDPDetailCollectionViewCell:0x7fbe55505a50]   (Names: '|':UICollectionView:0x7fbe538a0600 )>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cb20 h=--& v=--& 'UIView-Encapsulated-Layout-Top' V:|-(0)-[BDPDetailCollectionViewCell:0x7fbe55505a50]   (Names: '|':UICollectionView:0x7fbe538a0600 )>,
<NSLayoutConstraint:0x7fbe5550c690 'UIView-Encapsulated-Layout-Width' H:[BDPDetailCollectionViewCell:0x7fbe55505a50(90)]>
)

// After [super layoutSubViews]
(lldb) po self
<BDPDetailCollectionViewCell: 0x7fbe55505a50; baseClass = UICollectionViewCell; frame = (0 0; 90 64); clipsToBounds = YES; layer = <CALayer: 0x7fbe55505eb0>>

(lldb) po self.constraints
<__NSArrayI 0x7fbe550495e0>(
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cd30 h=--& v=--& UIView:0x7fbe555060e0.midX == + 45>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cd80 h=--& v=--& H:[UIView:0x7fbe555060e0(90)]>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cdd0 h=--& v=--& UIView:0x7fbe555060e0.midY == + 32>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550b850 h=--& v=--& V:[UIView:0x7fbe555060e0(64)]>,
<NSLayoutConstraint:0x7fbe555080d0 UIView:0x7fbe555060e0.trailing == UILabel:0x7fbe55506dd0'17.06'.trailing>,
<NSLayoutConstraint:0x7fbe55508120 UILabel:0x7fbe55506dd0'17.06'.leading == UIView:0x7fbe555060e0.leading>,
<NSLayoutConstraint:0x7fbe55508170 UILabel:0x7fbe55506dd0'17.06'.top == UIView:0x7fbe555060e0.top + 38>,
<NSLayoutConstraint:0x7fbe555081c0 UILabel:0x7fbe55507a40'本周市场份额'.leading == UIView:0x7fbe555060e0.leading>,
<NSLayoutConstraint:0x7fbe55508210 UIView:0x7fbe555060e0.trailing == UILabel:0x7fbe55507a40'本周市场份额'.trailing>,
<NSLayoutConstraint:0x7fbe55508260 UILabel:0x7fbe55507a40'本周市场份额'.top == UIView:0x7fbe555060e0.top + 15>,
<NSLayoutConstraint:0x7fbe5550c6e0 'UIView-Encapsulated-Layout-Height' V:[BDPDetailCollectionViewCell:0x7fbe55505a50(64)]>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cad0 h=--& v=--& 'UIView-Encapsulated-Layout-Left' H:|-(0)-[BDPDetailCollectionViewCell:0x7fbe55505a50]   (Names: '|':UICollectionView:0x7fbe538a0600 )>,
<NSAutoresizingMaskLayoutConstraint:0x7fbe5550cb20 h=--& v=--& 'UIView-Encapsulated-Layout-Top' V:|-(0)-[BDPDetailCollectionViewCell:0x7fbe55505a50]   (Names: '|':UICollectionView:0x7fbe538a0600 )>,
<NSLayoutConstraint:0x7fbe5550c690 'UIView-Encapsulated-Layout-Width' H:[BDPDetailCollectionViewCell:0x7fbe55505a50(90)]>
)

更新:添加屏幕截图显示:

enter image description here

0 个答案:

没有答案