iphone - tableView 上的绝对定位、 View 层次结构和重绘

标签 iphone objective-c ios uiview uinavigationcontroller

在我项目的主屏幕上,我有一个 TableView,顶部有一个导航栏,底部有一个工具栏。问题是,工具栏中的一个按钮需要从工具栏向上滑动一个 UISegmentedControl。我已经在自定义 UIView 中实现了这个控件,但是,如果我只是将它添加到我的 rootviewcontroller.view 并将其滑动到位,它将与表格的其余部分一起滚动,这是不希望的(我希望它显示为工具栏的扩展)。

那么,我该怎么办?在 rootViewController 中我做

self.filterView = [[FilterView alloc] initWithTarget:self.tableView reloadDataSelector:@selector(reloadData)];
[[self.view superview] addSubview:self.filterView];
[[self.view superview] bringSubviewToFront:self.filterView];

我将控件 View (self.filterView) 添加到我的 View 的父 View 中,并将其置于 TableView 的滚动条之上。

但是,问题来了。一旦 tableView 离开 View (我在 navigationController 上推送另一个 View ,或者特别是如果应用程序进入后台)这个 View 就会重新布局,并且我的 Controller View 会移动到 (0,0)。

问题是,就在 navigationController 上推送新 View 而言,我可以通过在我的 rootViewController 的 viewWillAppear 和 viewWillDisappear 中重新定位它来控制它。但是当应用程序进入后台并返回时,这些函数不会被调用。

那么,有什么办法可以

(a) 防止我的​​ Controller View 被移动

(b)检测它何时被无意中移动

(c)从 rootViewController 检测来自后台的进出

???

我知道我可以在 appDelegate 中检测到背景传递,但我不愿意在那里处理布局问题。

谢谢!!

编辑:

添加一些信息,如果我这样做的话

NSLog(@"%@",[self.view superview]); 

//I get <UIViewControllerWrapperView: 0x61589d0; frame = (0 64; 320 372); autoresize = W+H; layer = <CALayer: 0x615d9c0>>

EDIT2: 我想我可以先创建自己的包装器 UIView,在其中加载我当前的所有 View 层次结构,然后将 Controller View 放在那里。你们认为这值得麻烦吗?有没有更简单的方法?

EDIT3: 最终选择将我的 rootViewController 从 UITableViewController 更改为 UIViewController,并以编程方式添加了 tableView,正如 Phil 在下面所建议的那样。我现在可以随心所欲地控制我的主视图,并且由于我将我的 segmentedControl View 放在那里,我可以控制它的定位方式,而不是以前,当我将它放在 UIViewControllerWrapperView 中时,我不太确定谁控制它或者它对它的 subview 做了什么。

所以,出于好奇,有人知道为什么包装我的 UITableViewController View 的 UIViewControllerWrapperView 在从后台返回时移动我的 UIView 吗?

澄清一下,设置是这样的:

UIViewControllerWrapperView
  |
  |UITableView
  |Custom SegmentedControl UIView

最佳答案

作为旁注,您的代码中有一个模式,看起来该 View 将要泄漏,addSubview 会自动将该 View 置于 View 顺序的顶部。

但是,您的 View 滚动的原因是因为它被添加为 UITableView 的 subview ,而 UITableView 是 UIScrollView 的子类。当 ScrollView 滚动时,它将通过 contentOffset 属性向上或向下移动任何 subview 。当 UIScrollView 滚动时,它会重复布局其 subview 。由于 TableView 不知道您的自定义 subview ,因此它似乎只是将其移动到 0,0。

我假设您正在使用 UITableViewController。如果您计划为这个 View Controller 提供的不仅仅是一个 TableView ,那么您应该实现一个标准的 View Controller 。该 Controller 将有一个包含 TableView 和您的其他 View 的普通 View 。 UITableViewController 只是为了方便一个非常常见的情况。

如果您担心的话,很容易复制 UITableViewController 的功能。它实际上有非常清楚的记录。

When the table view is about to appear the first time it’s loaded, the table-view controller reloads the table view’s data. It also clears its selection (with or without animation, depending on the request) every time the table view is displayed. The UITableViewController class implements this in the superclass method viewWillAppear:. You can disable this behavior by changing the value in the clearsSelectionOnViewWillAppear property.

在您的实现中:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    if ([self.tableView numberOfSections] == 0) {
        [self.tableView reloadData];
    } else {
        [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:animated];
    }
}

When the table view has appeared, the controller flashes the table view’s scroll indicators. The UITableViewController class implements this in the superclass method viewDidAppear:.

在您的实现中:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.tableView flashScrollIndicators];
}

It implements the superclass method setEditing:animated: so that if a user taps an Edit|Done button in the navigation bar, the controller toggles the edit mode of the table.

在您的实现中:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

关于iphone - tableView 上的绝对定位、 View 层次结构和重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5572803/

相关文章:

javascript - 检测 iPhone 5 及其以下的任何 iOS 设备

ios - 我怎样才能让 CAAnimation 在每个动画节拍中调用一个 block ?

ios - 优化苹果系统日志

ios - Swift - 如何从照片库中获取最后拍摄的 3 张照片?

ios - 用动画剪出形状

iphone - 有没有办法自动更新 iPhone 应用程序?

iphone - 当观察者变为零时观察者会自动移除吗?

iphone - 未调用 didSelectRowAtIndexPath。 UITableViewController 子类化

Objective-C – ABMultiValueRef 获取类型 (ABAddressBook)

objective-c - UIActionSheet showInView 上的 iPad NSInternalInconsistencyException