1

我想在我的 UITextFields 底部有一个两行 UILabel,作为免责声明。

我有以下代码viewDidLoad

    UILabel *InstapaperSubscriptionLabel = [[UILabel alloc] init];

    InstapaperSubscriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
    InstapaperSubscriptionLabel.text = @"Requires an Instapaper subscription, please see the Instapaper website for more details.";
    InstapaperSubscriptionLabel.numberOfLines = 2;
    InstapaperSubscriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
    InstapaperSubscriptionLabel.preferredMaxLayoutWidth = self.view.bounds.size.width - 40;
    InstapaperSubscriptionLabel.textAlignment = NSTextAlignmentCenter;
    InstapaperSubscriptionLabel.backgroundColor = [UIColor clearColor];
    InstapaperSubscriptionLabel.textColor = [UIColor colorWithRed:112/255.0 green:112/255.0 blue:111/255.0 alpha:1.0];
    InstapaperSubscriptionLabel.font = [UIFont systemFontOfSize:14.0];

    [self.view addSubview:InstapaperSubscriptionLabel];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:InstapaperSubscriptionLabel
                                                 attribute:NSLayoutAttributeTop
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.passwordBox
                                                 attribute:NSLayoutAttributeBottom
                                                 multiplier:1.0
                                                 constant:15.0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:InstapaperSubscriptionLabel
                                                 attribute:NSLayoutAttributeLeading
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                                 attribute:NSLayoutAttributeLeft
                                                 multiplier:1.0
                                                 constant:20.0]];

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:InstapaperSubscriptionLabel
                                                 attribute:NSLayoutAttributeTrailing
                                                 relatedBy:NSLayoutRelationEqual
                                                 toItem:self.view
                                                 attribute:NSLayoutAttributeRight
                                                 multiplier:1.0
                                                 constant:20.0]];

但每次我加载视图时,它看起来像这样:

在此处输入图像描述

它离开屏幕一侧的地方。为什么要这样做?我希望它更居中。

4

1 回答 1

2

我认为您在问题中显示的最后一个约束的常数应该是-20,而不是 20。您用该代码所说的是:

label trailing = superview right * 1 + 20 这将使标签的后沿离开屏幕 20 点向右。

于 2013-07-28T17:09:12.693 回答