swift - 将 View 添加为 subview 时,ContentInset 未更新

标签 swift uitableview layout uinavigationbar ios9

我正在制作一个照片浏览应用程序,用户可以在其中查看正在查看的照片的详细信息和评论。为此,我有一个带有分段控件的 UIViewController (parentVC),充当详细信息和注释之间的开关,以及两个提供详细信息和注释的 UITableViewController。 Storyboard of the view controllers in question .

要在 View 之间切换,tableVC 会从 Storyboard 实例化,并作为 childViewController 添加到 ParentVC,并且它们的 View 会添加为 subview 。

由于segmentedControl放置在navigationController的navBar中,我希望tableViews也能与parentVC的navBar很好地配合。其中当第一个tableview(detailVC)被实例化时,它的contentInset会自动调整。

但是,当从分段控件切换后实例化第二个 TableView (commentsVC) 时,它的 contentInset 未设置(事实上,所有边都为 0),并且 TableView 隐藏在导航栏后面。但是当设备旋转时,桌面 View 会调整其插图,并且再次正常工作。

现在我怀疑这个问题与iOS如何自动调整scrollviewinsets有关,因为切换第一个出现的childVC总是会设置第一个出现的VC的contentInset。我不知道添加第二个 VC 时是否可以触发任何方法来设置它的 contentInset。

所以问题是,如何将子VC的contentInset设置为尊重parentVC中的navigationBar?创建 UIEdgeInset 是一种解决方法,但当设备方向更改时它不会自行更新。一个更小的问题是,当以编程方式实例化 childVC 时,我应该如何使用导航 Controller 的布局和插入,以便它们像在 Storyboard本身中链接一样工作。

以下是我从父VC实例化子VC时的代码。

    if index == 0 { //details

        if detailsVC == nil {
            detailsVC = storyboard?.instantiateViewControllerWithIdentifier("InfoPaneDetailsVC") as! InfoPaneDetailsVC
        }
        if commentsVC != nil {
            commentsVC.view.removeFromSuperview()
            commentsVC.removeFromParentViewController()
        }

        addChildViewController(detailsVC)
        detailsVC.didMoveToParentViewController(self)
        view.addSubview(detailsVC.view)
        view.layoutSubviews()

    } else if index == 1 { //comments

        if commentsVC == nil {
            commentsVC = storyboard?.instantiateViewControllerWithIdentifier("InfoPaneCommentsVC") as! InfoPaneCommentsVC
        }
        if detailsVC != nil {
            detailsVC.view.removeFromSuperview()
            detailsVC.removeFromParentViewController()
        }

        addChildViewController(commentsVC)
        commentsVC.didMoveToParentViewController(self)
        view.addSubview(commentsVC.view)
        view.layoutSubviews()

    }

最佳答案

the tableview is hidden behind the navBar. But when the device is rotated, the tableview adjust its insets and it works fine again.

我可以复制您所看到的内容。我不知道为什么旋转设备会消除下重叠*。我什至可以横向启动应用程序,TableView 将与导航栏重叠,然后当我旋转到纵向时,重叠就会消失。

有几个解决方案适合我:

  1. edgesForExtendedLayout = .None
  2. navigationController?.navigationBar.translucent = false

如果 ViewController 的父级是 NavigationController(或 TabBarController),则两个属性决定下重叠的工作方式:

  1. ViewController 的 edgesForExtendedLayout 属性。
  2. 导航栏的半透明属性。

edgesForExtendedLayout 属性的默认值为 .All,这意味着 ViewController 的 View 将覆盖所有 半透明条。

您可以像这样检查您的导航栏是否半透明:

import UIKit

class InfoViewController: UIViewController {

     override func viewDidLoad() {
        super.viewDidLoad()

        if navigationController?.navigationBar.translucent == true {
            print("NavigationBar is translucent.")
        }

        if edgesForExtendedLayout == .All {
            print("This ViewController's view will underlap a translucent NavigationBar.")
        }

我得到:

NavigationBar is translucent.
This ViewController's view will underlap a translucent NavigationBar.

UINavigationBar docs**

Additionally, navigation bars are translucent by default. Turning the translucency off or on does not affect buttons, since they do not have backgrounds.

您可以像这样防止下重叠:

import UIKit

class InfoViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        edgesForExtendedLayout = .None
    }

.None 告诉 ViewController 的 View 不要重叠任何半透明条。或者,您可以使用 .Bottom,这意味着 View 将仅位于半透明底部栏下方,即不是半透明顶部栏。 (要查看可以分配给 edgesForExtendedLayout 的各种值(或值的组合),请查看 docs )。

或者,您可以像这样防止下重叠:

import UIKit

class InfoViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationController?.navigationBar.translucent = false
    }

edgesForExtendedLayout 的值仅适用于半透明条—— View 不会覆盖不透明条。如果您不想在代码中将半透明设置为 false,则可以选择 Storyboard中的导航栏,然后在属性检查器中取消选中半透明。

<小时/>

* 旋转设备后,我打印了 edgesForExtendedLayout 和导航栏的 translucent 属性,但它们没有改变:

class InfoViewController: UIViewController {

    override func viewWillTransitionToSize(size: CGSize,
        withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
    {
        super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

        //executes before transition

        coordinator.animateAlongsideTransition(//unlabeled first argument:
            { (UIViewControllerTransitionCoordinatorContext) -> Void in
                //executes during transition
            },
            completion: { (UIViewControllerTransitionCoordinatorContext) -> Void in
                //executes after transition

                print("After rotating:")
                if self.navigationController?.navigationBar.translucent == true {
                    print("NavigationBar is translucent.")
                }

                if self.edgesForExtendedLayout == .All {
                    print("This ViewController's view will underlap a translucent NavigationBar.")
                }

            }
        )
    }

--output:--
NavigationBar is translucent.
This ViewController's view will underlap a translucent NavigationBar.
After rotating:
NavigationBar is translucent.
This ViewController's view will underlap a translucent NavigationBar.

** 如果导航栏有背景图像,则导航栏的半透明属性的值将根据图像的 Alpha 设置为 true 或 false。请参阅UINavigationBar docs 中的半透明度 部分。 .

关于swift - 将 View 添加为 subview 时,ContentInset 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37963382/

相关文章:

objective-c - iOS - 从 cell.textLabel.frame 获取 CGRect

CSS Flex - 如果内容更宽,列会切换到行吗?

css - 使用 flexbox 的多列和多行布局

ios - 如何将模型数据对象添加到 Firebase 数据库

swift - 如何使用swift在heroku app上上传图片

swift - 如何在 NumberFormatter 中的逗号后显示零

swift - TvOS 焦点引擎跳过单元格

ios - 在 UITableViewCell 中重用 UITextView

layout - Controller 如何在 magento 2 中加载布局

swift CATransition : Is there anyway to remove the fading effect from this animation?