【发布时间】:2016-04-16 18:17:59
【问题描述】:
我在 UIScrollView 中的 UIView 中有一个 UILabel。我应该看到两件事:
- UILabel 到达屏幕右侧后水平环绕
- UIScrollView 不能水平滚动
但是我目前看到的东西:
- 单词不会环绕,并且滚动视图允许我水平滚动。
在进行了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