2017-10-20 18 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()糾正這種

相關問題