首页 > 解决方案 > 布局锚和 NSLayoutConstraint 不起作用?

问题描述

我曾经(现在仍然)在玩 Objective-C。我想学习如何以编程方式创建 UI。我有一个方法采用已经定义的 UIButton(我不知道定义是否是正确的词)并根据开发人员的输入对其进行自定义。这是功能:

   +(void)configureButtonWithBoldTextTop50Left150RightAnchor160: (UIButton *)buttonToConfigure whithXParam: (CGFloat)x withYParam: (CGFloat)y withHeight: (CGFloat)height withWigth: (CGFloat)width  withTitle: (NSString *)title withTitleColor: (UIColor *)titleColor withCornerRadius: (CGFloat)cornerRadius withBorderWidth: (CGFloat)borderWidth withBorderColor: (UIColor *) borderColor withButtonBackgroundColor: (UIColor *)buttonBackgroundColor withFontSize:(CGFloat)fontSize presentIN: (UIViewController *)ViewControllerToPresentIn 
{

        CGFloat leadingAnchorValue = 150; // left
        CGFloat trailingAnchorValue = -160; // right

        buttonToConfigure = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonToConfigure.translatesAutoresizingMaskIntoConstraints = NO;
        buttonToConfigure = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [buttonToConfigure setTitle:title forState:UIControlStateNormal];
        [buttonToConfigure.titleLabel setFont:[UIFont fontWithName:@"Helveticaneue-Bold" size:fontSize]];
        [buttonToConfigure setTitleColor:titleColor forState:UIControlStateNormal];
        [buttonToConfigure setFrame: CGRectMake(x, y, width, height)];

        buttonToConfigure.layer.cornerRadius = cornerRadius;
        buttonToConfigure.clipsToBounds = YES;
        buttonToConfigure.layer.borderWidth = borderWidth;
        buttonToConfigure.layer.borderColor = borderColor.CGColor;
        buttonToConfigure.backgroundColor = buttonBackgroundColor;
        buttonToConfigure.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

        // adding button to view hierarchy

        [ViewControllerToPresentIn.view addSubview:buttonToConfigure];

        [buttonToConfigure.centerXAnchor constraintEqualToAnchor:ViewControllerToPresentIn.view.centerXAnchor].active = YES;
        [buttonToConfigure.topAnchor constraintEqualToAnchor:ViewControllerToPresentIn.view.topAnchor constant:50].active = YES;
        [buttonToConfigure.trailingAnchor constraintEqualToAnchor:ViewControllerToPresentIn.view.trailingAnchor constant:trailingAnchorValue].active = YES;
        [buttonToConfigure.leadingAnchor constraintEqualToAnchor:ViewControllerToPresentIn.view.leadingAnchor constant:leadingAnchorValue].active = YES;
    }

问题是布局锚不起作用。此外,我尝试通过在我的 .h 文件中定义(创建)它来添加 NSLayoutConstraint,但是当我将它添加到按钮时,整个应用程序崩溃并出现错误:“线程 1:信号 SIGABRT”。如何添加某种约束,使其在所有屏幕尺寸上看起来都一样?

标签: iosobjective-c

解决方案


您正在创建按钮两次。删除以下行,因为您正在使用约束:

buttonToConfigure = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];

还有一种更简单的方法可以一次设置一堆约束:

UIView *view = ViewControllerToPresentIn.view;
[view addSubview:buttonToConfigure];

[NSLayoutConstraint activateConstraints:@[
    [buttonToConfigure.centerXAnchor constraintEqualToAnchor:ViewControllerToPresentIn.view.centerXAnchor]
    [buttonToConfigure.topAnchor constraintEqualToAnchor:view.topAnchor constant: 50],
    [buttonToConfigure.heightAnchor constraintEqualToConstant: height],
    [buttonToConfigure.widthAnchor constraintEqualToConstant: width],
]];

请注意,这里我提供了不同的约束。你需要决定你真正需要哪些约束。

请记住,您需要指定水平位置和大小以及垂直位置和大小。我在这里展示的约束只是满足该要求的几种方法之一。

您问题中的限制有两个问题。

  1. 您有三个水平约束。由于您有前导和尾随,因此centerXAnchor没有必要使用 。前导和尾随约束定义了水平位置和大小。
  2. 您只有一个垂直约束。对于 UIButton 这可能不是问题,因为按钮具有默认高度。但是您的方法需要一个height您不使用的参数(在删除额外的按钮创建行之后)。

您的方法具有 x、y、宽度和高度,但您的任何约束都不使用任何这些参数。所以你需要决定什么是必要的。


推荐阅读