ios - UIViewController 隐藏在 UINavigationController 后面,但应该放在下面

标签 ios swift uiview view uinavigationcontroller

我正在开发一个应用程序,我在顶部有一个导航栏,并添加了一个 UIViewController 作为 RootViewController。

现在我的计划是向此 UIViewController 添加一个新的 subview 。新的 subview 也扩展了 UIViewController。我将 Controller 的 View (灰色矩形)添加到 UIViewController 中,但它放置在导航栏后面。我不想这样..所以我寻找解决方案并发现了一些有趣的东西..: 当我只是将 UIView() (绿色矩形)添加到 UIViewController 时, View 的放置效果非常好,因为我希望从其他 UIViewController-View 中看到它。 enter image description here 我的代码如下所示:

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)
 }}

我已经取消选中“扩展边缘” - Storyboard上导航 Controller 的切换。我还在 UIViewController 中添加了edgesForExtendedLayout = [],对于 UIView 来说它工作得很好。但对于另一个 UIViewController 的 View ......它不起作用。

谢谢!

最佳答案

如果您使用调试 View 层次结构,您将看到灰色 ccview.view 不在导航栏后面,而是没有保持 self.view.frame.width / 2 的高度.

这是因为.viewUIViewController从 Storyboard 实例化的默认情况下有 .autoresizingMask = [] ,而.viewUIViewController在没有 Storyboard的情况下实例化具有默认值 .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)
}

关于ios - UIViewController 隐藏在 UINavigationController 后面,但应该放在下面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46856743/

相关文章:

ios - Snapkit,将 UILabel 粘到底部

ios - 在 Collection View 中的单个单元格中播放视频

ios - 打印对象的强所有者,Swift

ios - 如何将主包与测试包调配

ios - WKWebView 添加为 subview

ios - 仅适用于 iOS5 的 SIGABRT 消息

swift - tableview 下方的 iAd 横幅

iphone - 可以在 iPad 上计算 powerpoint 演示文稿的宽度/高度吗

iphone - endTurnWithNextParticipant 弃用

ios - 如何子类化 UI 元素,如 UILabel、UIButton