【发布时间】:2015-11-25 10:39:36
【问题描述】:
在 iOS 8.0 中看到一些关于自动布局行为的奇怪行为,并正在寻找有关如何解决此问题的任何提示或帮助。我有一个 UIView 包含另一个 UIView 作为包含两个标签的子视图。这些标签的约束以编程方式添加如下:
//StreamCaptionTextView.m
- (void)updateConstraints
{
NSDictionary *views = @{
@"title" : self.titleLabel,
@"desc" : self.descLabel
};
NSDictionary *metrics = @{
@"top" : @0,
@"middle" : @(-2)
};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[title]-0-|" options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[desc]-0-|" options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[title]-middle-[desc(==title)]" options:0 metrics:metrics views:views]];
[super updateConstraints];
}
这个 labelView 的父视图也为 labelView 设置了约束。
//StreamCaptionView which contains the StreamCaptionTextView
- (void)updateConstraints
{
[self removeConstraints:self.constraints];
NSMutableDictionary *views = [[NSMutableDictionary alloc] initWithDictionary:@{
@"image" : self.profileImageBtn,
@"leftView" : self.leftText
}];
NSDictionary *metrics = @{
@"top" : @6,
@"left" : @8,
@"imageSize" : @24,
@"marginText" : @10,
@"maxWidth" : @120
};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[image(imageSize)]" options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[leftView]-top-|" options:0 metrics:metrics views:views]];
if(self.rightAcessoryView)
{
views[@"rightView"] = self.rightAcessoryView ;
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-left-[image(imageSize)]-2-[leftView]-10-[rightView(maxWidth)]-10-|" options:0 metrics:metrics views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-top-[rightView]-top-|" options:0 metrics:metrics views:views]];
}
else
{
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-marginText-[image(imageSize)]-2-[leftView]-marginText-|" options:0 metrics:metrics views:views]];
}
[super updateConstraints];
}
//This is the View that contains the captionView
- (void)setVo:(RecordingStreamItemVO *)vo
{
_vo = vo;
__weak RecordingDetailVideoView *_self = self;
self.captionView.title = vo.user.screenname;
self.captionView.subTitle = vo.title;
self.captionView.profileImageURL = vo.user.pictureURL;
//Here, the captionView is the rectangular blur, inside of the
//captionView is another UIView that contains the UILabels.
}
由于某种原因,只有在 iOS 8.0 上,标签没有在 labelView 中正确呈现。这适用于 iOS 8.1 和 7.1 等。我尝试使用 [self.labelView setNeedsUpdateConstraints] 使约束无效,但这似乎没有 解决问题。我已经用谷歌搜索了很多,我看到其他人在 iOS 8.0 上遇到了 autoLayout 问题,但想验证这是否是 iOS 问题以及我可以实施的任何潜在解决方法。
//这里有一些照片展示了它在 Xcode 可视化调试器中的样子
//第一张照片在iOS 8.1上运行正常
//这张照片是在 iOS 8.0 上,似乎忽略了约束。
//ios 8.0
layer = <CALayer: 0x17396c90>>
| | | | | | | | | | <StreamCaptionView: 0x15f1fe10; frame = (0 146; 320 35); layer = <CALayer: 0x15f2b3e0>>
| | | | | | | | | | | <UIButton: 0x15fcc240; frame = (10 6; 24 24); opaque = NO; layer = <CALayer: 0x15fcc340>>
| | | | | | | | | | | | <UIImageView: 0x1700d1c0; frame = (0 0; 24 24); clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x1700d240>>
| | | | | | | | | | | <StreamCaptionTextView: 0x15fcc410; frame = (36 6; 274 23); layer = <CALayer: 0x15f2f620>>
| | | | | | | | | | | | <MarqueeLabel: 0x15f2f650; baseClass = UILabel; frame = (0 0; 274 13.5); text = 'test1'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x15f200b0>>
| | | | | | | | | | | | | <UILabel: 0x17378d20; frame = (0 0; 26 0); text = 'test1'; userInteractionEnabled = NO; tag = 700; animations = { position=<CAKeyframeAnimation: 0x173b8960>; }; layer = <_UILabelLayer: 0x17378de0>>
| | | | | | | | | | | | | <UILabel: 0x173906b0; frame = (45 0; 22 0); text = 'test1'; userInteractionEnabled = NO; tag = 701; animations = { position=<CAKeyframeAnimation: 0x173b8a60>; }; layer = <_UILabelLayer: 0x17390770>>
| | | | | | | | | | | | <MarqueeLabel: 0x17390c40; baseClass = UILabel; frame = (0 11.5; 274 13.5); text = 'Test'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x17390d60>>
| | | | | | | | | | | | | <UILabel: 0x17390940; frame = (0 0; 17 0); text = 'Test'; userInteractionEnabled = NO; tag = 700; animations = { position=<CAKeyframeAnimation: 0x173b93a0>; }; layer = <_UILabelLayer: 0x17390a00>>
| | | | | | | | | | | | | <UILabel: 0x173a1640; frame = (82 0; 48 0); text = 'Test'; userInteractionEnabled = NO; tag = 701; animations = { position=<CAKeyframeAnimation: 0x173b94a0>; }; layer = <_UILabelLayer: 0x173a1700>>
| | | | | | | | | <_UITableViewCellSeparatorView: 0x173ae010; frame = (15 179.5; 305 0.5); layer = <CALayer: 0x173ad1d0>>
| | | | | | | | <TimePublishedDetailView: 0x173a1d60; frame = (238.5 20; 71.5 11); layer = <CALayer: 0x173a1ef0>>
| | | | | | | | | <UIImageView: 0x173a21e0; frame = (0 0; 9 9); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x173a2260>>
| | | | | | | | | <UILabel: 0x173a85d0; frame = (16 -2; 55.5 13.5); text = '5 hours ago'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x173a8550>>
更新:
我实际上在父视图控制器中通过一个非常简单的修复解决了这个问题。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view addSubview:self.detailsView];
[self.view setNeedsUpdateConstraints];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self loadDetails];
[self.view layoutIfNeeded]; //This fixed the iOS 8.0 problem
}
//In my case the self.detailsView property contains the view hierarchy which
//contains the UILabels that weren't being rendered properly way down
//in the view hierarchy. calling layoutIfNeeded fixes this problem for me.
【问题讨论】:
-
我知道这不是您要找的东西,但曾经尝试过吗? github.com/smileyborg/PureLayout 它确实简化了编写约束的过程,并使任何阅读它们的人都更加直观
-
您是否尝试过使用 View Debugger 对其进行调试?您应该能够看到到底发生了什么,视图是如何定位的以及正在播放哪些约束
-
@sha 我已经包含了一些 Xcode 的可视化调试器的照片。我可以看到,在 iOS 8.0 中,约束位置与 8.1 中不同。
-
我认为有一个已知问题是
removeConstraints无法正常工作是 iOS8.0 你可以尝试修改你的代码以预先设置约束并在运行时修改它们吗? -
我注意到您的标签高度为 0。或许问题就在于此。我已经更新了答案。
标签: ios objective-c iphone ios8