【问题标题】:UIViewController hidden behind UINavigationController but should be placed belowUIViewController 隐藏在 UINavigationController 后面,但应该放在下面
【发布时间】:2018-04-02 01:49:57
【问题描述】:

我正在开发一个顶部有 NavigationBar 并添加 UIViewController 作为 RootViewController 的应用程序。

现在我的计划是向这个 UIViewController 添加一个新的 Subview。新的子视图也扩展了 UIViewController。我将控制器的视图(灰色矩形)添加到 UIViewController,但它位于 NavigationBar 后面。我不想要那个..所以我搜索了一个解决方案并发现了一些有趣的东西..: 当我只是将 UIView()(绿色矩形)添加到 UIViewController 时,视图的放置效果很好,因为我希望从另一个 UIViewController-View 中看到它。 我的代码如下所示:

class DashboardController: UIViewController {

var ccview:ContactCircleController!

override func viewDidLoad() {
    super.viewDidLoad()
    edgesForExtendedLayout = []
    self.view.backgroundColor = .white

    let test = UIView()
    test.backgroundColor = .green
    test.frame = CGRect(x: self.view.frame.width - 200, y: 0, width: self.view.frame.width / 2, height: self.view.frame.width / 2)
    self.view.addSubview(test)
    setup()
}

func setup(){
    ccview = ContactCircleController()

    ccview.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width / 2, height: self.view.frame.width / 2)
    ccview.edgesForExtendedLayout = UIRectEdge.top
    self.view.addSubview(ccview.view)
 }}

我已经取消选中“扩展边缘” - 故事板上导航控制器的切换。我还向 UIViewController 添加了 edgesForExtendedLayout = [] ,对于 UIView 它工作正常。但是对于另一个 UIViewController 的视图......它没有用。

谢谢!

【问题讨论】:

    标签: ios swift uiview view uinavigationcontroller


    【解决方案1】:

    如果您使用 Debug View Hierarchy,您会看到您的灰色 ccview.view 不在导航栏后面,而是没有保持 self.view.frame.width / 2 的高度。

    这是因为从 Storyboard 实例化的 UIViewController.view 默认具有 .autoresizingMask = [],而在没有 Storyboard 的情况下实例化的 UIViewController.view 具有默认的 .autoresizingMask = [.flexibleWidth, .flexibleHeight]

    您可以通过将setup() 更改为:

    func setup(){
        ccview = ContactCircleController()
    
        ccview.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width / 2, height: self.view.frame.width / 2)
    
        // remove this line
        //ccview.edgesForExtendedLayout = UIRectEdge.top
    
        // add this line
        ccview.view.autoresizingMask = []
    
        self.view.addSubview(ccview.view)
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多