【发布时间】:2015-05-05 15:50:54
【问题描述】:
我已将UIButton 和UILabel 添加到UIScrollView。我使用自动布局向按钮添加了两个约束,而对标签则没有。我可以看到标签在屏幕上移动,但是我没有看到按钮在移动。我认为这与我添加到按钮的自动布局约束有关。我希望看到按钮滚动的方式与我看到标签在窗口/屏幕上滚动的方式相同。
以下是我如何设置所有内容:
_welcomeScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
_welcomeScroller.userInteractionEnabled = YES;
_welcomeScroller.scrollEnabled = YES;
_welcomeScroller.showsHorizontalScrollIndicator = YES;
_welcomeScroller.showsVerticalScrollIndicator = YES;
#ifdef DEBUG
[_welcomeScroller setBackgroundColor:[UIColor greenColor]];
#endif
[self.view addSubview:_welcomeScroller];
CGSize welcomeScrollerSize = CGSizeMake(2000, 2000);
[_welcomeScroller setContentSize:welcomeScrollerSize];
// add test label
_test = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 200, 200)];
[_test setText:@"TEST"];
[_test setFont:[UIFont systemFontOfSize:44]];
[_test setBackgroundColor:[UIColor redColor]];
[_welcomeScroller addSubview:_test];
// add about btn to lower right
_welcomeAbout = [UIButton buttonWithType:UIButtonTypeInfoDark];
[_welcomeAbout addTarget:self action:@selector(showAboutScreen:) forControlEvents:UIControlEventTouchUpInside];
[_welcomeAbout setTranslatesAutoresizingMaskIntoConstraints:NO];
[_welcomeScroller addSubview:_welcomeAbout];
NSLayoutConstraint *pullToBottom = [NSLayoutConstraint constraintWithItem:_welcomeAbout attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_welcomeScroller.superview attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.0];
NSLayoutConstraint *pullToRight = [NSLayoutConstraint constraintWithItem:_welcomeAbout attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_welcomeScroller.superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10];
[_welcomeScroller.superview addConstraints:@[pullToBottom, pullToRight]];
【问题讨论】:
标签: ios objective-c autolayout