【问题标题】:Why can I not create a multi-line UILabel with Auto Layout? Something always is off为什么我不能使用自动布局创建多行 UILabel?总有一些事情是关闭的
【发布时间】:2013-07-28 16:59:39
【问题描述】:

我想在我的 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]];

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

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

【问题讨论】:

    标签: ios objective-c uiview uilabel autolayout


    【解决方案1】:

    我认为您在问题中显示的最后一个约束的常数应该是 -20,而不是 20。 你对这段代码的意思是:

    label trailing = superview right * 1 + 20,这会将标签的后沿向右偏离屏幕 20 个点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-19
      • 2014-11-17
      • 1970-01-01
      相关资源
      最近更新 更多