为什么同一个frame的root view和subview的边距不一样?

问题描述 投票:0回答:1

我只是想沿着两个子视图的边缘创建一个彩色边框,其中一个在 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 )>"
)

问题/问题

  1. 如您所见,只有红色边框呈现,因为蓝色边框由于上面显示的错误(以及其他几个与其他锚点类似的错误)而无法呈现。
  2. 这个约定是否正确?如果没有,我什至可以实现它的方式吗?即尝试匹配 ViewController 中根视图的子视图和 UIView 子类 (LibraryView) 的子视图的边距

如果这是一个被误导的问题,我们深表歉意。 iOS 开发在概念上有点棘手,我正在尝试在 Objective-C 中进行,因此在线资源更加有限。提前致谢。

ios objective-c xcode uiview layoutmargins
1个回答
0
投票

问题是你的

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],
]];
© www.soinside.com 2019 - 2024. All rights reserved.