gpt4 book ai didi

iphone - objective-c - 横向 UIView 自动调整大小

转载 作者:行者123 更新时间:2023-11-28 22:55:21 25 4
gpt4 key购买 nike

我有一个包含 3 个 View 的 viewController:topView、tableView 和 textView。

我希望他们这样布局:

在纵向模式下 -

|---------------|  
| topView |
|---------------|
| |
| |
| tableView |
| |
| |
|---------------|
| textView |
|---------------|

横向模式:

|--------------------------|  
| topView |
|--------------------------|
| |
| tableView |
|--------------------------|
| textView |
|--------------------------|

当键盘启动时:

|---------------|  
| |
| tableView |
| |
|---------------|
| textView |
|---------------|
| |
| keyboard |
| |
|---------------|

|--------------------------|
| tableView |
|--------------------------|
| textView |
|--------------------------|
| |
| keyboard |
|--------------------------|

(如您所见,当键盘打开时顶 View 消失了)

执行此操作的代码是:

- (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) {
//
}];
}

我想要的大部分都运行良好,问题是当键盘以横向模式显示时,然后就会出错..

请注意 View 的 autoresizingMask 属性,因为我不确定我当前是否设置了它。

最佳答案

对于自动旋转,尝试将顶 View 的 mask 设置为灵活宽度,中间的 mask 设置为灵活宽度和灵活高度,以及 TextView 、灵活宽度和上边距。我怀疑这是否会顺利进行,但值得一试。您可以覆盖 View Controller 上的 willAnimateRotationToInterfaceOrientation:duration: 以进行自定义工作。我很确定基本的自动旋转蒙版无法处理这种布局,但我很乐意被证明是错误的。

同时检查 loadView 中的方向,如下所示:

if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
// do one layout
} else {
// do another layout
}

我也在我的应用程序中做非常自定义的旋转工作(从头开始想想自定义 Split View Controller ——糟糕),我正在使用 willAutorotateUIInterfaceOrientationIsLandscape.

这可能不适用于您,因为您对问题的措辞方式:我不确定问题是否出在您打开键盘,然后旋转到横向(在这种情况下,此答案可能会有所帮助);或者当您横向打开键盘时是否遇到问题(在这种情况下,此答案可能不太适合您)。

关于iphone - objective-c - 横向 UIView 自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com