【问题标题】:UIRefreshControl hidden / obscured by my UINavigationController's UINavigationBarUIRefreshControl 被我的 UINavigationController 的 UINavigationBar 隐藏/遮挡
【发布时间】:2013-01-22 05:45:53
【问题描述】:

我正在尝试在我的UITableViewController 中使用UIRefreshControl,它本身位于UINavigationController 中,它的hidesNavigationBar 属性设置为NO(因此导航栏可见)。

UIRefreshControl 有效,但被UINavigationBar 遮挡。我很惊讶我找不到其他遇到此问题的人。

可能的相关点:

  • 我将UIWindowrootViewController 设置为我的UINavigationController
  • 我通过设置UINavigationControllerviewControllers 属性来设置UINavigationController 的初始视图控制器。
  • 我的 UITableViewController 子类是用 nib 实例化的。
  • 我在我的UITableViewController 子类的viewDidLoad 方法中实例化我的UIRefreshControl。我在这个方法中设置了UITableViewController 子类的refreshControl 属性。
  • UIRefreshControl 工作得非常好,我可以看到其中的一部分,但它被我的UINavigationBar 遮住了。如果我将hidesNavigationBar 设置为YES,它看起来完全正常(但我不想隐藏它)。

编辑:

用于创建和定位我的UIRefreshControl 的代码是:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self 
                   action:@selector(toggleRefresh:) 
         forControlEvents:UIControlEventValueChanged];
self.refreshControl = refreshControl;

这段代码 sn-p 在我的UITableViewController 子类的viewDidLoad 方法中,它是UINavigationViewController 的子视图控制器。

【问题讨论】:

  • 尝试在 viewWillAppear 中设置刷新控件。
  • 好主意,但没用。
  • 然后尝试在笔尖设置刷新控制。在 Attributes Inspector 的 Table View Controller 下,您可以启用 Refreshing。
  • 奇怪的是,当我选择文件的所有者时,属性检查器会显示“不适用”。 File's Owner 的类是UITableView 的子类。无论如何,我认为它是在笔尖中设置还是以编程方式设置并不重要。我看到UIRefreshControl,只是位置不正确。
  • 你能把代码贴在你创建和放置控件的地方吗?

标签: ios uitableview cocoa-touch uinavigationcontroller uirefreshcontrol


【解决方案1】:

使用@Jonathan 的回答,我得到了很好的效果。但由于我使用的是故事板,所以我将内容插入设置为:以防万一有人需要:

(xcode 6.4)

【讨论】:

    【解决方案2】:

    我找到了一个真正的解决方案,这里是:

    我在UINavigationController 中有一个UIViewController 和一个半透明的NavigationBar。在UIViewController 里面有UITableView

    我想添加一个UIRefreshControl,但是当我这样做时,它被NavigationBar 隐藏了,就像你解释的那样。

    这是我让它工作的代码:

    // Add a UITableViewController
    self.tblViewController = [[UITableViewController alloc] init];
    
    // Set the UITableView to it
    self.tblViewController.tableView = self.tblView;
    
    // Initialize the UIRefreshControl and set it's method
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
    
    // Set the RefreshControl to the UITableViewController
    self.tblViewController.refreshControl = self.refreshControl;
    
    // Here is the thing ! Just change the contentInset to push down the UITableView content to 64 pixels (StatusBar + NavigationBar)
    self.tblView.contentInset = UIEdgeInsetsMake(64.f, 0.f, 0.f, 0.f);
    

    使用此代码,您的UITableViewController 将完美显示RefreshControl,并在您向下滚动单元格时保持半透明NavigationBar 效果。

    【讨论】:

      【解决方案3】:

      在iOS 7中,self.view是在navigationBar下,只不过你写的东西如下,

      self.edgesForExtendedLayout = UIRectEdgeNone; // or UIRectEdgeAll & ~UIRectEdgeTop
      

      self.navigationViewController.navigationbar.translucent = NO;
      

      【讨论】:

        【解决方案4】:

        对于那些以 iOS 7 为目标的用户,似乎存在一个新问题,UIRefreshControl 被绘制在 UITableViewbackgroundView 后面。我在以编程方式初始化UIRefreshControl 时和从情节提要中都经历了这一点。一个简单的解决方法是在您的UITableViewControllerviewDidLoad 中更新UIRefreshControlzPosition

        self.refreshControl.layer.zPosition = self.tableView.backgroundView.layer.zPosition + 1;
        

        【讨论】:

        • 非常感谢您的回答。我有一个添加了背景视图(图像)的表格视图,我花了几个小时试图弄清楚为什么没有显示刷新控件!
        • 谢谢,但问题是现在控制在桌子上。我试过 self.tableView.backgroundView.layer.zPosition =self.refreshControl.layer.zPosition- 1;但没有成功。
        • @Ricardo 我相信这是 iOS 7 中的预期行为。您必须更改表格视图的偏移量才能在动画时显示滚动指示器。
        • 如果您不想弄乱图层的 zPosition,您也可以通过确保在设置 tableView 的 backgroundView 之后初始化 refreshControl 来解决这个问题。跨度>
        • @BartVandendriessche 这对我不起作用,我什至尝试在 -viewDidApear 最后一行尝试这样做,但没有成功。
        【解决方案5】:

        请勿在您的UINavigationBar 上拨打-setTranslucent:。然后,您的刷新控件将正确定位在导航栏下方。

        【讨论】:

          【解决方案6】:

          这对我来说似乎是一个错误,因为它仅在 tableView 的 contentOffset 属性为 0 时发生

          看到这个问题

          UIRefreshControl not showing spiny when calling beginRefreshing and contentOffset is 0

          我使用以下代码(UITableViewController 的方法)解决了这个问题:

          - (void)beginRefreshingTableView {
          
              [self.refreshControl beginRefreshing];
          
              if (self.tableView.contentOffset.y == 0) {
          
                  [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^(void){
          
                      self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
          
                  } completion:^(BOOL finished){
          
                  }];
          
              }
          }
          

          【讨论】:

          • 这是最好的解决方案
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多