NSLayoutConstraint标签位于图像中间

时间:2014-11-06 04:27:20

标签: objective-c nslayoutconstraint

我将UIImageView和UILabel附加到UIScrollView。我将图像缩小到它的高度的一半,当我这样做时,标签最终在图像的中间垂直而不是在它的正下方。如果我不强制缩小图像,标签会按预期位于图像的正下方。

如何缩小图像并让我的标签仍位于其下方?

UIScrollView *scrollView;
UIImageView *myImage;
NSDictionary *viewsDictionary;
UILabel *myLabel;

scrollView = [[UIScrollView alloc] init];
myImage = [[UIImageView alloc] init];
myLabel = [[UILabel alloc] init];

[myImage setImage:[UIImage imageNamed:@"vacation-house-004.jpg"]];
[myImage setContentMode: UIViewContentModeScaleAspectFill];
[myLabel setText: @"test label"];

[self.view addSubview:scrollView];

[scrollView addSubview:myImage];
[scrollView addSubview:myLabel];

scrollView.translatesAutoresizingMaskIntoConstraints = NO;
myImage.translatesAutoresizingMaskIntoConstraints = NO;
myLabel.translatesAutoresizingMaskIntoConstraints = NO;

// Set the constraints for the scroll view and the image view.
viewsDictionary = NSDictionaryOfVariableBindings(scrollView, myImage, myLabel);
[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:|[myImage(==scrollView)]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[myImage(50)]-[myLabel]|" options:0 metrics: 0 views:viewsDictionary]];

谢谢!

1 个答案:

答案 0 :(得分:0)

想出来,不得不添加:myImage.clipsToBounds = YES;

UIView *superView = self.view;
UIScrollView *scrollView;
UIImageView *myImage;
NSDictionary *viewsDictionary;
UILabel *myLabel;

scrollView = [[UIScrollView alloc] init];
myImage = [[UIImageView alloc] init];
myLabel = [[UILabel alloc] init];

myImage.image = [UIImage imageNamed:@"vacation-house-004.jpg"];
myImage.contentMode = UIViewContentModeScaleAspectFill;
myImage.clipsToBounds = YES;

myLabel.text = @"test label";
myLabel.backgroundColor = [UIColor blackColor];

[superView addSubview:scrollView];

[scrollView addSubview:myImage];
[scrollView addSubview:myLabel];

scrollView.translatesAutoresizingMaskIntoConstraints = NO;
myImage.translatesAutoresizingMaskIntoConstraints = NO;
myLabel.translatesAutoresizingMaskIntoConstraints = NO;

// Set the constraints for the scroll view and the image view.
viewsDictionary = NSDictionaryOfVariableBindings(superView, scrollView, myImage, myLabel);
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[superView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[myImage(==scrollView)]|" options:0 metrics: 0 views:viewsDictionary]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[myImage(150)]-[myLabel]|" options:0 metrics: 0 views:viewsDictionary]];