gpt4 book ai didi

ios - 自动布局问题

转载 作者:行者123 更新时间:2023-11-28 22:24:22 26 4
gpt4 key购买 nike

我在 View Controller 的 Root View 上有以下自动布局约束:

(lldb) po [superview constraints]
<__NSArrayM 0x9e65ba0>(
<NSLayoutConstraint:0x9e67d10 H:[HeaderView:0x9e6a1b0]-(0)-| (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67ce0 H:|-(0)-[HeaderView:0x9e6a1b0] (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67cb0 V:|-(0)-[HeaderView:0x9e6a1b0] (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67c80 H:|-(0)-[RadialGradientView:0x9e685c0] (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67c50 V:[RadialGradientView:0x9e685c0]-(0)-| (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67c20 H:[RadialGradientView:0x9e685c0]-(0)-| (Names: '|':UIView:0x9e69040 )>,
<NSLayoutConstraint:0x9e67bf0 V:[HeaderView:0x9e6a1b0]-(0)-[RadialGradientView:0x9e685c0]>,
<NSLayoutConstraint:0x10c89830 HeaderView:0x9e6a1b0.height == UIView:0x9e69040.height + 35>
)

当我添加高度约束(最后一个)时,我得到一个自动布局模糊布局错误:

2013-10-16 16:18:43.121 Application[26038:a0b] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0xb016da0 V:|-(0)-[HeaderView:0xb0192a0] (Names: '|':UIView:0xb018130 )>",
"<NSLayoutConstraint:0xb016d40 V:[RadialGradientView:0xb0176b0]-(0)-| (Names: '|':UIView:0xb018130 )>",
"<NSLayoutConstraint:0xb016ce0 V:[HeaderView:0xb0192a0]-(0)-[RadialGradientView:0xb0176b0]>",
"<NSLayoutConstraint:0xb00c3f0 HeaderView:0xb0192a0.height == UIView:0xb018130.height + 35>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xb016ce0 V:[HeaderView:0xb0192a0]-(0)-[RadialGradientView:0xb0176b0]>

Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

RadialGradientView 是 UIView 的子类,它只是重写了 drawRect: 方法。HeaderView 也是 UIView 的子类,代码如下:

@implementation HeaderView
{
NSLayoutConstraint * heightConstraint;
}

static UINib * headerViewNib = nil;

- (id)awakeAfterUsingCoder:(NSCoder *)aDecoder
{
if (![[self subviews] count])
{
if (headerViewNib == nil)
headerViewNib = [UINib frameworkNibWithNibName: @"HeaderView"];

SMDFHeaderView * headerView = [[headerViewNib instantiateWithOwner: nil options: nil] lastObject];

[headerView setTranslatesAutoresizingMaskIntoConstraints: NO];
[self removeConstraints: [self constraints]];

return headerView;
}
return self;
}

- (void)layoutSubviews
{
if (!heightConstraint)
[self setNeedsUpdateConstraints];

[super layoutSubviews];
}

- (void)updateConstraints
{
if (!heightConstraint)
{
UIView * superview = [self superview];
heightConstraint = [NSLayoutConstraint constraintWithItem: self attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: superview attribute:NSLayoutAttributeHeight multiplier: 1.0 constant: 35.0];
[superview addConstraint: heightConstraint];
}

[super updateConstraints];
}

/* Tried that, but it does not change anything.
- (CGSize)intrinsicContentSize
{
return CGSizeMake([[super superview] frame].size.width, 35);
}*/

@end

PS:这个问题似乎与 Nib 更换技巧无关,即使我评论 awakeAfterUsingCoder 方法,它也做同样的事情。

最佳答案

你误解了这条线的工作原理:

heightConstraint = [NSLayoutConstraint constraintWithItem: self attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: superview attribute:NSLayoutAttributeHeight multiplier: 1.0 constant: 35.0];

这样做是让 self 等于 superview 的高度,乘以 1,再加上 35。这意味着你要求 subview 为 35 点比父 View 高,并且在其他地方,要求 subview 适合父 View 。那是行不通的。

要将 View 限制在特定高度,您需要使用如下内容:

heightConstraint = [NSLayoutConstraint constraintWithItem: self attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: nil attribute:NSLayoutAttributenNotAnAttribute multiplier: 1.0 constant: 35.0];

这只是将 View 限制在特定高度。

关于ios - 自动布局问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19405915/

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