简体   繁体   English

iOS - 提供控制器时UITableView位置重置

[英]iOS - UITableView Position Resets When Controller is Presented

So I have a view controller that has a UITableView that covers the bottom half of the controller. 所以我有一个视图控制器,它有一个覆盖控制器下半部分的UITableView I have a button in the top section header that allows the user to minimize or maximize the UITableView . 我在顶部标题中有一个按钮,允许用户最小化或最大化UITableView When the button is pressed the y component of the UITableView 's position is modified so that the UITableView appears to have minimized to the bottom of the screen. 按下按钮时, UITableView位置的y分量被修改,以便UITableView看起来最小化到屏幕底部。 When the button is pressed while the UITableView is in its minimized state the UIableView 'sy position is returned to its original value. UITableView处于最小化状态时按下按钮时, UIableView的sy位置将返回其原始值。 Both the maximizing and minimizing of the UITableView are animated to provide a smooth transition between the opened and closed states of the UITableView . 这两个的最大化最小化和UITableView的动画提供的开闭状态之间的平滑过渡UITableView

This all works very nicely and provides a user experience that allows the user to utilize the information displayed in the table view and the information displayed within the view controller behind it. 这一切都非常好用,并提供用户体验,允许用户利用表格视图中显示的信息和其后面的视图控制器中显示的信息。 The problem comes when a second controller is presented over top. 第二个控制器出现在顶部时出现问题。 For some reason when the user dismisses the second controller the table view always returns to its initial position regardless of the position it was in at the time the second controller was presented. 出于某种原因,当用户解除第二个控制器时,无论第二个控制器出现时的位置如何,表格视图始终返回其初始位置。 This creates many issues as the initial view controller still thinks the table view is in the position it was at the time of presentation. 这会产生许多问题,因为初始视图控制器仍然认为表视图处于演示时的位置。 Why is this? 为什么是这样? Why are the positions of components reset when a view controller is presented over top and then dismissed? 当视图控制器显示在顶部然后被解除时,为什么组件的位置会重置?

Here is how I defined my UITableView as a property: 以下是我将UITableView定义为属性的方法:

@property (nonatomic, strong) IBOutlet UITableView *reviewTable;

Here is how I minimize/maximize my table view: 以下是我最小化/最大化表格视图的方法:

- (IBAction)btnExpandCompress:(id)sender
{
    UIImage *downImage = [UIImage imageNamed:@"arrowDown.png"];
    UIImage *upImage = [UIImage imageNamed:@"arrowUp.png"];
    UIButton *btnExpandCompress = (UIButton *)sender;
    [btnExpandCompress setEnabled:NO];

    //global flag to tell if the table view is maximized or minimized
    if (showingReviews)
    {
        [UIView animateWithDuration:0.7 animations:^{
            CGRect frame = reviewTable.frame;
            frame.origin.y += reviewTable.frame.size.height - 30;
            reviewTable.frame = frame;
        } completion:^(BOOL finished) {
            showingReviews = NO;
            [btnExpandCompress setEnabled:YES];
            [btnExpandCompress setBackgroundImage:upImage forState:UIControlStateNormal];
        }];
    }
    else
    {
        [UIView animateWithDuration:0.7 animations:^{
            CGRect frame = reviewTable.frame;
            frame.origin.y -= reviewTable.frame.size.height - 30;
            reviewTable.frame = frame;
        } completion:^(BOOL finished) {
            showingReviews = YES;
            [btnExpandCompress setEnabled:YES];
            [btnExpandCompress setBackgroundImage:downImage forState:UIControlStateNormal];
        }];
    }
}

If there is any other information needed to answer this question please let me know. 如果有任何其他信息需要回答这个问题,请告诉我。

As I cannot comment, I'll have to add this as an answer. 由于我无法评论,我将不得不添加这个作为答案。

Open your storyboard->View Controller(Which has the table view)-> View of the View controller. 打开storyboard->View Controller(Which has the table view)-> View视图控制器的视图。

Inside this view, if the tableView is first in the view hierarchy from the top, the table view cells shift downwards to the height of the navigation bar. 在此视图中,如果tableView是从顶部开始的视图层次结构中的第一个,则表格视图单元格向下移动到导航栏的高度。

Hope this helps! 希望这可以帮助!

So it turned out that the reason that the UITableView was returning to its original position was because of issues regarding auto layout. 事实证明,UITableView返回其原始位置的原因是因为有关自动布局的问题。 When I disabled auto layout in the storyboard file the UITableView behaved correctly. 当我在storyboard文件中禁用自动布局时,UITableView表现正常。 This caused a number of other issues in my application's UI so I settled on disabling auto layout for just the UITableView in question. 这在我的应用程序的UI中引起了许多其他问题,因此我决定仅针对有问题的UITableView禁用自动布局。 This worked, and here is the code I used to do it: 这工作,这是我用来做的代码:

//This disables autolayout for myTableView
myTableView.translatesAutoresizingMaskIntoConstraints = YES;

Feel free to comment if there are any issues with my solution that I have overlooked. 如果我的解决方案存在任何我忽略的问题,请随时发表评论。 If not I will accept this answer as it seems to have solved my problem. 如果不是,我会接受这个答案,因为它似乎解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现imagePicker时,iOS动画textView返回原始位置吗? - iOS animated textView returns to original position when imagePicker is presented? “viewWillTransitionToSize:”当视图控制器以模态呈现时不会在 iOS 9 中调用 - "viewWillTransitionToSize:" not called in iOS 9 when the view controller is presented modally iOS 关闭模态呈现视图时黑屏 controller - iOS Black screen when close modally presented view controller UITableView内容在滚动时重置 - UITableView Contents resets when scrolling iOS:reloadData 时将 UITableView 保持在同一位置 - iOS: Keep UITableView at the same position when reloadData 当UITableView和UIImageView都显示在同一视图控制器上时,如何正确使用约束 - How to correctly use constraints when both UITableView and UIImageView are presented on the same view controller iOS:在uitableview中跳一个位置 - IOS: jump a position in a uitableview 单元测试在iOS中呈现了视图控制器和键盘 - Unit testing presented view controller and keyboard in iOS iOS取消了模态呈现的导航控制器 - iOS dismissing a modally presented Navigation Controller 在iOS中从提供的视图控制器推送视图 - Push View from Presented View Controller in iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM