无法在 UIScrollView 中将 UILabel 包装在 UIView 中

Cannot wrap UILabel in UIView within UIScrollView

我在 UIScrollView 中的 UIView 中有一个 UILabel。我应该看到两件事:

  1. UILabel 到达屏幕右侧后水平环绕
  2. UIScrollView 不水平滚动

但是我目前看到的东西:

  1. 单词不会环绕,滚动视图允许我水平滚动。

经过 this one 等研究后,我无法在我的项目中看到预期的结果,但我在如下所示的简化代码中看到了相同的结果。

当前层级是

-UIScrollView
    -UIView
        -UILabel

代码如下。我错过了什么?

- (void)viewDidLoad {
    [super viewDidLoad];


    UIScrollView * scrollView = [UIScrollView new];
    scrollView.translatesAutoresizingMaskIntoConstraints = false;
    scrollView.backgroundColor = [UIColor lightGrayColor];

    [self.view addSubview: scrollView];
    [self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[scrollView]|"
                                                                        options: 0
                                                                        metrics: nil
                                                                          views: NSDictionaryOfVariableBindings(scrollView)]];

    [self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[scrollView]|"
                                                                        options: 0
                                                                        metrics: nil
                                                                          views: NSDictionaryOfVariableBindings(scrollView)]];

    UIView * view = [UIView new];
    view.translatesAutoresizingMaskIntoConstraints = false;


    UILabel * label = [UILabel new];
    label.backgroundColor = [UIColor yellowColor];
    label.numberOfLines = 0;
    label.translatesAutoresizingMaskIntoConstraints = false;
    label.text = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

    [scrollView addSubview: view];
    [scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[view]|"
                                                                       options: 0
                                                                       metrics: nil
                                                                         views: NSDictionaryOfVariableBindings(view)]];

    [scrollView addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[view]|"
                                                                        options: 0
                                                                        metrics: nil
                                                                          views: NSDictionaryOfVariableBindings(view)]];


    [view addSubview: label];
    [view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[label]|"
                                                                        options: 0
                                                                        metrics: nil
                                                                          views: NSDictionaryOfVariableBindings(label)]];

    [view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[label]|"
                                                                        options: 0
                                                                        metrics: nil
                                                                          views: NSDictionaryOfVariableBindings(label)]];





    // Do any additional setup after loading the view, typically from a nib.
}

你应该像这样给出约束,

1) 滚动视图 - 顶部、底部、前导、尾随

2) View - top,bottom,leading,trailing,fixed height,horizo​​ntally center in container (If want vertical scrolling) or fixed width, vertically center (if want horizo​​ntal scrolling) - Scrollview 将滚动如果视图高度大于屏幕尺寸或 self.view(在垂直滚动的情况下)同样如果宽度大于则滚动将在水平方向完成

3) 标签 - top,bottom,leading,trailing or top,leading,trailing,fix height which is suited according to requirement.

检查您的限制条件。和上面说的一致吗?

还有一件事,您可以使用 UITextview 和编辑禁用来显示允许滚动的大字符串。

希望这会有所帮助:)

在使用@Lion 关于使用水平居中的建议并添加了这段代码后,它起作用了:

[scrollView addConstraint: [NSLayoutConstraint constraintWithItem: view
                                                        attribute: NSLayoutAttributeCenterX
                                                        relatedBy: NSLayoutRelationEqual
                                                           toItem: scrollView
                                                        attribute: NSLayoutAttributeCenterX
                                                       multiplier: 1.0
                                                          constant: 0]];

整个脚本变为:

- (void)viewDidLoad {
    UIScrollView *scrollView;
    UILabel * label;
    UIView * view;
    UIImageView *imageView;
    NSDictionary *viewsDictionary;

    // Create the scroll view and the image view.
    scrollView  = [[UIScrollView alloc] init];
    //imageView = [[UIImageView alloc] init];
    label = [UILabel new];
    label.text = @"SDF ksaf lkahsdf kashflasf kjsdhfakljf lsd asdfas asdf saf saf asdf asd fasfs ka";
    label.numberOfLines = 0;

    view = [UIView new];
    view.backgroundColor = [UIColor yellowColor];

    // Add an image to the image view.
    //[imageView setImage:[UIImage imageNamed:"MyReallyBigImage"]];

    // Add the scroll view to our view.
    [self.view addSubview:scrollView];

    // Add the image view to the scroll view.
    //[scrollView addSubview:imageView];
    [scrollView addSubview: view];

    // Set the translatesAutoresizingMaskIntoConstraints to NO so that the views autoresizing mask is not translated into auto layout constraints.
    scrollView.translatesAutoresizingMaskIntoConstraints  = NO;
    imageView.translatesAutoresizingMaskIntoConstraints = NO;
    label.translatesAutoresizingMaskIntoConstraints = NO;
    view.translatesAutoresizingMaskIntoConstraints = NO;
    [view addSubview: label];

    // Set the constraints for the scroll view and the image view.
    viewsDictionary = NSDictionaryOfVariableBindings(scrollView, view, label);
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics: 0 views:viewsDictionary]];
    [scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics: 0 views:viewsDictionary]];
    [scrollView addConstraint: [NSLayoutConstraint constraintWithItem: view
                                                            attribute: NSLayoutAttributeCenterX
                                                            relatedBy: NSLayoutRelationEqual
                                                               toItem: scrollView
                                                            attribute: NSLayoutAttributeCenterX
                                                           multiplier: 1.0
                                                              constant: 0]];
    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[label]|" options:0 metrics: 0 views:viewsDictionary]];
    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[label]|" options:0 metrics: 0 views:viewsDictionary]];


    /* the rest of your code here... */
}