I have a UILabel in a UIView that's within UIScrollView. There are two things I should see:
- UILabel wraps around horizontally after reaching the right side of screen
- The UIScrollView to not scroll horizontally
But the things that I currently see:
- The word does not wrap around and the scrollview allows me to scroll horizontally.
After doing some research like this one, I am unable to see the expected result in my project and I see the same result in a simplified code like below.
The current hierarchy is
-UIScrollView
-UIView
-UILabel
and the code is below. What am I missing?
- (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.
}