У меня есть viewController с 3 представлениями: 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) {
//
}];
}
Большая часть того, что мне нужно, работает хорошо, проблема в том, что когда клавиатура отображается в ландшафтном режиме, все идет не так..
Обратите внимание на свойство views autoresizingMask
, потому что я не уверен, что установил его сейчас.