【问题标题】:Cannot wrap UILabel in UIView within UIScrollView无法在 UIScrollView 中的 UIView 中包装 UILabel
【发布时间】:2016-04-16 18:17:59
【问题描述】:

我在 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.
}

【问题讨论】:

    标签: objective-c uiscrollview autolayout uilabel


    【解决方案1】:

    你应该给出类似的约束,

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

    2) 视图 - 顶部、底部、前导、尾随、固定高度、水平居中在容器中(如果想要垂直滚动)或固定宽度、垂直居中(如果想要水平滚动) - 如果视图高度更大,则滚动视图将滚动然后屏幕大小或 self.view(在垂直滚动的情况下)同样明智,如果宽度更大,则滚动将在水平方向完成

    3) 标签 - 顶部、底部、前导、尾随或顶部、前导、尾随、固定高度,根据要求适合。

    检查您的约束。和上面说的一致吗?

    另外,您可以使用 UITextview 禁用编辑来显示允许滚动的大字符串。

    希望这会有所帮助:)

    【讨论】:

    • 这是 2) 在容器中水平居中修复它
    • 如果您觉得它有帮助,请接受答案并投票,以便其他有问题的人可以使用它。 :)
    • 您的答案包含一个答案,但它过于笼统。我查看了一段时间以查看可能缺少的组件并最终找到它,然后我发布了完整的代码。在那种情况下,我是在找到根本原因后接受你的答案还是我的答案?
    • 最好把根或基本概念搞清楚。添加一两行代码来复制和粘贴所有代码是没有意义的。我试图以这种方式回答,任何错过约束的人都可以使用。就像您错过水平中心一样,有人可能会错过另一个。顺便说一句,不客气:)
    【解决方案2】:

    在使用@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... */
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多