我只是尝试沿着两个子视图的边缘创建一个彩色边框,其中一个在 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(subview,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.正如您所看到的,只有红色边框渲染,因为蓝色边框由于上面显示的错误(以及其他锚点的其他几个类似的错误)而无法渲染。
1.这个惯例是否正确?如果没有,我实现它的方式是否可能?即试图匹配ViewController中根视图的子视图和UIView子类(LibraryView)中的子视图的边距
如果这是一个误导性的问题,我道歉。iOS开发在概念上有点棘手,我试图用Objective-C来做,所以在线资源更加有限。先谢谢你了。
1条答案
按热度按时间vshtjzan1#
问题出在
LibraryView
的initWithFrame
上。您正试图将库视图的约束设置为自身。从
LibraryView
中删除所有约束相关代码。您希望在根视图控制器的viewDidLoad
中设置约束。行后:
您可以添加代码来设置
library
的约束,该约束与用于设置l3
的margins
变量相同。此外,激活一组约束更容易,如下所示: