2017-10-20 15 views
0
以下

我正在开发一个应用程序,其中我在顶部有一个NavigationBar,并将一个UIViewController添加为RootViewController。UIViewController隐藏在UINavigationController后面,但应放在

现在我的计划是添加一个新的子视图到这个UIViewController。新的Subview也扩展了UIViewController。我将控制器的de view(Gray Rect)添加到了UIViewController,但它放在NavigationBar后面。我不想那样..所以我搜索了一个解决方案,并发现了一些有趣的..: 当我只是添加一个UIView()(绿色矩形)的UIViewController,视图的放置完美的作品,因为我会爱从另一个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) 
}} 

我已经取消选中了“扩展边缘” - 在navigationcontroller的切换上的脚本。我也添加edgesForExtendedLayout = []到UIViewController和UIView它工作正常。但对于另一个UIViewController的视图...它没有奏效。

谢谢!

回答

0

如果您使用调试视图层次,你会看到你的灰色ccview.view导航栏后面不,而是它不保持的self.view.frame.width/2高度。

这是因为从故事板实例化的UIViewController.view具有默认的.autoresizingMask = [],而没有一个情节串连图板实例化的UIViewController.view具有默认.autoresizingMask = [.flexibleWidth, .flexibleHeight]

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

你可以通过改变setup()纠正这种

相关问题