pai*_*aiv 9 landscape uiscrollview ios autolayout
我正在与autolayout(iOS 6,7)中的scrollview进行一段时间的斗争,而且它正在变得令人沮丧.
我想要滚动,这应该在横向调整大小:




看哪!经过2天的搜索,我相信我可能会有一个答案.诚然,没有代码就无法完成.
首先从contentView到Scroll视图创建常用的top,bottom,leading和trailing约束.但是使用前导和尾随,勾选"占位符 - 在构建时删除"选项.
然后在viewDidLoad方法中添加以下内容:
NSLayoutConstraint *leftConstraint =[NSLayoutConstraint
constraintWithItem:self.contentView
attribute:NSLayoutAttributeLeading
relatedBy:0
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0];
[self.view addConstraint:leftConstraint];
NSLayoutConstraint *rightConstraint =[NSLayoutConstraint
constraintWithItem:self.contentView
attribute:NSLayoutAttributeTrailing
relatedBy:0
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0];
[self.view addConstraint:rightConstraint];
Run Code Online (Sandbox Code Playgroud)
这会动态地将contentView中的前导和尾随约束添加到控制器的主视图(即滚动视图之外).
然后,当您旋转设备时,输入字段将被适当拉伸.这解决了你的旋转问题,关于键盘出现在SO上的其他答案,但基本上在viewDidLoad内:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardDidHideNotification object:nil];
Run Code Online (Sandbox Code Playgroud)
然后添加以下两种方法:
- (void) keyboardWasShown:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
- (void) keyboardWillBeHidden:(NSNotification *)notification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8052 次 |
| 最近记录: |