我有一个带有 3 个视图的 viewController:topView、tableView 和 textView。
我希望他们像这样布局:
在纵向模式下 -
|---------------|
| topView |
|---------------|
| |
| |
| tableView |
| |
| |
|---------------|
| textView |
|---------------|
在横向模式下:
|--------------------------|
| topView |
|--------------------------|
| |
| tableView |
|--------------------------|
| textView |
|--------------------------|
当键盘启动时:
|---------------|
| |
| tableView |
| |
|---------------|
| textView |
|---------------|
| |
| keyboard |
| |
|---------------|
|--------------------------|
| tableView |
|--------------------------|
| textView |
|--------------------------|
| |
| keyboard |
|--------------------------|
(如您所见,当键盘启动时,顶视图消失了)
这样做的代码是:
- (void)loadView
{
[super loadView];
// load top view
_topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, TOP_VIEW_HEIGHT)];
_topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
_topView.backgroundColor = [UIColor redColor];
[self.view addSubview:_topView];
// load table view (at the middle)
CGRect tableFrame = CGRectMake(0, TOP_VIEW_HEIGHT, self.view.bounds.size.width, self.view.bounds.size.height - TOP_VIEW_HEIGHT - TEXT_VIEW_HEIGHT);
_tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
_tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
// load text view (at the bottom)
CGRect textViewFrame = CGRectMake(0, tableFrame.size.height + TOP_VIEW_HEIGHT, self.view.bounds.size.width, TEXT_VIEW_HEIGHT);
_textView = [[UITextView alloc] initWithFrame:textViewFrame];
_textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
_textView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_textView];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
// Get animation info from userInfo
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect textViewFrame = self.textView.frame;
textViewFrame.origin.y -= kbSize.height;
CGRect topViewFrame = self.topView.frame;
topViewFrame.origin.y -= topViewFrame.size.height;
CGRect tableFrame = self.tableView.frame;
tableFrame.origin.y -= topViewFrame.size.height;
tableFrame.size.height -= (kbSize.height - topViewFrame.size.height);
[UIView animateWithDuration:animationDuration delay:0.0 options:animationCurve animations:^{
self.textView.frame = textViewFrame;
self.tableView.frame = tableFrame;
self.topView.frame = topViewFrame;
}completion:^(BOOL finished) {
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
// Get animation info from userInfo
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
CGSize kbSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect textViewFrame = self.textView.frame;
textViewFrame.origin.y += kbSize.height;
CGRect topViewFrame = self.topView.frame;
topViewFrame.origin.y += topViewFrame.size.height;
CGRect tableFrame = self.tableView.frame;
tableFrame.origin.y += topViewFrame.size.height;
tableFrame.size.height += kbSize.height - topViewFrame.size.height;
[UIView animateWithDuration:animationDuration delay:0.0 options:animationCurve animations:^{
self.textView.frame = textViewFrame;
self.tableView.frame = tableFrame;
self.topView.frame = topViewFrame;
}completion:^(BOOL finished) {
//
}];
}
我想要的大部分工作都很好,问题是当键盘以横向模式显示时,事情就会出错..
请注意viewsautoresizingMask
属性,因为我不确定我当前是否设置了它。