无法在UIScrollView中的UIView中包装UILabel

时间:2016-04-16 18:18:00

标签: objective-c uiscrollview autolayout uilabel

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

  1. UILabel在到达屏幕右侧后水平环绕
  2. UIScrollView不能水平滚动
  3. 但是我目前看到的东西:

    1. 单词没有环绕,滚动视图允许我水平滚动。
    2. 在完成像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.
      }
      

2 个答案:

答案 0 :(得分:1)

你应该给出约束,

1)Scrollview - 顶部,底部,前导,尾随

2)视图 - 顶部,底部,前导,尾随,固定高度,水平居中于容器中(如果需要垂直滚动)或固定宽度,垂直居中(如果想要水平滚动) - 如果视图高度较大,则滚动查看然后屏幕尺寸或自我视图(在垂直滚动的情况下),如果宽度更大,那么滚动将在水平上完成

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

检查您的约束条件。它与上面提到的相符吗?

另外一件事你可以使用UITextview编辑禁用来显示允许滚动的大字符串。

希望这会有所帮助:)

答案 1 :(得分:0)

在使用@ Lion关于使用水平居中的建议后,我已经添加了这段代码,它可以运行:

SELECT * FROM Customer WHERE visitTime > =  convert(date,getdate()) 
and visitTime <  dateadd(d,1,convert(date,getdate())) 

整个脚本变为:

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