提问人:ennell 提问时间:4/22/2023 更新时间:4/23/2023 访问量:44
为什么根视图和同一框架的子视图的边距不同?
Why are the margins of the root view and a subview with the same frame different?
问:
我只是尝试沿着两个子视图的边缘创建一个彩色边框,其中一个在 ViewController 中定义,另一个在 UIView 子类中定义。两者都是根视图的子视图。我想使第二个子视图(在 UIView 子类中创建)中的蓝色边框与第一个子视图(在 ViewController 中创建)中的红色边框匹配。
ViewController(根视图)
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// add border subview (red)
UILayoutGuide *margins = self.view.layoutMarginsGuide;
self.view.translatesAutoresizingMaskIntoConstraints = NO;
UIView *l3 = [[UIView alloc] init];
[self.view addSubview:l3];
l3.layer.borderWidth = (CGFloat)1.0;
l3.layer.borderColor = UIColor.systemRedColor.CGColor;
l3.translatesAutoresizingMaskIntoConstraints = NO;
[l3.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
[l3.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
[l3.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YES;
[l3.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].active = YES;
// add border subview from LibraryView class (blue)
LibraryView *library = [[LibraryView alloc] initWithFrame:self.view.frame];
[self.view addSubview:library];
}
LibraryView(子视图,UIView 子类)
@implementation LibraryView
// LibraryView inherits from UIView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self != nil)
{
// add margins of this LibraryView object
UILayoutGuide *margins = self.layoutMarginsGuide;
self.layer.borderWidth = (CGFloat)1.0;
self.layer.borderColor = UIColor.systemBlueColor.CGColor;
self.translatesAutoresizingMaskIntoConstraints = NO;
[self.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;
[self.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;
[self.topAnchor constraintEqualToAnchor:margins.topAnchor].active = YES;
[self.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor].active = YES;
}
return self;
}
@end
结果
错误
<UILayoutGuide: 0x600000a282a0 - "UIViewLayoutMarginsGuide", layoutFrame = {{0, 0}, {0, 0}}, owningView = <LibraryView: 0x144f06c70; frame = (0 0; 393 852); layer = <CALayer: 0x60000336dc80>>>
2023-04-22 10:51:48.573386-0400 VoiceMemos[1043:18123] [LayoutConstraints] 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.
(
"<NSLayoutConstraint:0x60000102bd40 H:[UILayoutGuide:0x600000a282a0'UIViewLayoutMarginsGuide']-(0)-| (active, names: '|':LibraryView:0x144f06c70 )>",
"<NSLayoutConstraint:0x600001012580 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600000a282a0'UIViewLayoutMarginsGuide']-(8)-|(LTR) (active, names: '|':LibraryView:0x144f06c70 )>"
)
问题/问题
- 正如你所看到的,只有红色边框呈现,因为蓝色边框由于上面显示的错误(以及其他几个类似的锚点)而无法呈现。
- 这个约定正确吗?如果没有,我实施它的方式是否有可能?即尝试匹配 ViewController 中根视图的子视图的边距和 UIView 子类 (LibraryView) 中的子视图的边距
如果这是一个误导性的问题,我们深表歉意。iOS 开发在概念上有点棘手,我正在尝试用 Objective-C 进行开发,因此在线资源更加有限。提前致谢。
答:
1赞
HangarRash
4/23/2023
#1
问题出在你的 .您正在尝试将库视图的约束设置为自身。initWithFrame
LibraryView
从 中删除所有与约束相关的代码。您希望在根视图控制器中设置约束。LibraryView
viewDidLoad
行后:
[self.view addSubview:library];
您可以添加代码来针对用于设置的同一变量设置约束。library
margins
l3
此外,激活一堆约束更容易,如下所示:
[NSLayoutConstraint activateConstraints:@[
[l3.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor],
[l3.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor],
[l3.topAnchor constraintEqualToAnchor:margins.topAnchor],
[l3.bottomAnchor constraintEqualToAnchor:margins.bottomAnchor],
]];
评论
0赞
enell
4/24/2023
感谢您的回复!这是有道理的,但作为原始问题的后续,如果我使用根视图的框架初始化 LibraryView,为什么 LibraryView 中的边距无论如何都不同?它们不应该是一样的吗?无论如何,您的实现确实解决了原始问题。
0赞
HangarRash
4/24/2023
永远不应该依赖视图的框架。始终使用约束来处理由多种原因引起的视图大小调整。viewDidLoad
评论