【问题标题】:Update tableView's headerView on device rotation更新 tableView 标题查看设备旋转
【发布时间】:2015-11-02 01:53:58
【问题描述】:

我有一个带有自定义标题视图的表格视图。在标题视图中,我有一个 UIImage 子视图。在旋转时,标题视图及其子视图不会更新到它们应该在的位置。相反,它们会保留自己的位置,直到用户按下按钮、移动到不同的页面等。本质上,它们的位置不会更新,直到用户以某种方式与应用交互。

我正在检测设备旋转:

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        // Update header views!
    }

我找不到任何可以更新标题视图及其子视图的代码。任何帮助表示赞赏。

编辑:这是我的标题视图的代码:

  override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, tableView.sectionHeaderHeight))
        let headerColor = UIColor.blueColor()
        headerView.backgroundColor = headerColor

        headerView.tag = section

        let headerString = UILabel(frame: CGRect(x: 10, y: 10, width: tableView.frame.size.width-45, height: 30)) as UILabel
        headerString.text = "Title"
        headerString.textColor = UIColor.whiteColor()
        headerView.addSubview(headerString)
        let headerTapped = UITapGestureRecognizer(target: self, action:"sectionHeaderTapped:")
        headerView.addGestureRecognizer(headerTapped)

        // Button in header
        let gearImage = UIImage(named: "icons_button")
        let width = CGFloat(45)
        let rightInset = CGFloat(10)
        let headerButton = UIButton(frame: CGRect(x: headerView.frame.maxX - 45, y: 7, width: width, height: width - rightInset))
        headerButton.setImage(gearImage, forState: UIControlState.Normal)
        headerButton.tag = section
        headerButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: rightInset)
        headerButton.addTarget(self, action: "userButtonPress:", forControlEvents: UIControlEvents.TouchUpInside)
        headerView.addSubview(headerButton)

        return headerView
    }

【问题讨论】:

  • 你在使用故事板吗?
  • 是的,我正在使用故事板
  • 您考虑过使用自动布局吗?您可以在情节提要或代码中设置约束

标签: ios swift uitableview


【解决方案1】:

在 viewForHeaderInSection 中添加一个类似 UIVIEW 的容器 然后在容器中添加像标签一样的子视图 添加这一行

label.autoresizingMask = .flexibleWidth

【讨论】:

    【解决方案2】:

    虽然看起来有点矫枉过正,但效果很好:

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
      [coordinator animateAlongsideTransition: ^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
        [_myTableView reloadData];
      } completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {
        [_myTableView reloadData];
      }];
    }
    

    【讨论】:

    • 这对我有帮助。但是为什么会有两次重载呢?在动画关闭期间我最终得到了reloadData,并保持了完成处理程序的空闲。
    • 我猜这取决于您的标题视图代码,但有时动画步骤不包括最终位置,所以完成重新加载一旦完成就一切顺利。
    【解决方案3】:

    在你的旋转函数中:

    self.view.layoutIfNeeded()
    

    【讨论】:

    • 这并不能解决我的问题。我尝试在不同的视图上使用 layoutIfNeeded() 但仍然没有变化。
    • 你的页眉框架是如何设置的?自动布局还是以编程方式?
    • 标题框架和它的子视图是用 CGRect 以编程方式设置的
    • 我假设您还记得在您的viewWillTransitionToSize 中设置这些的功能?或者创建一个新框架,例如header.frame = CGRect(x: pageCenter, y: 0, width: pageWidth, height: 50)?
    • 我想我需要重新加载标题视图?我不知道在viewWillTransitionToSize 中调用什么来重新加载表格视图中的标题
    猜你喜欢
    • 2020-09-24
    • 2014-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多