2016-12-22 3 views
1

UITabBarControllerをサブクラス化していて、タブバーの中央にボタンを追加しようとしています。最初はうまくアップを示しています。私は私のTableViewControllerで項目を選択するとTabBarの前にUIButtonを置いておきます

enter image description here

はその後、それが表示されたまま:

さらに

enter image description here

、私は戻ってTableViewControllerに移動するとき、タブバーの後ろに表示されます。

enter image description here

これは私がボタンを追加するために使用しているコードです:

class PhotoTabBarController: UITabBarController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
    addPhotoButton() 
} 

// ... 

func addPhotoButton() { 
    let button = UploadButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60)) 


    // Start of code to center button in the tab bar 

    let heightDifference = button.frame.size.height - self.tabBar.frame.size.height; 
    // if the height of the center button is less than the height of the tab bar... 
    if (heightDifference < 0) { 
     // center the button inside the tab bar 
     button.center = self.tabBar.center; 
    } else { 

     var center = self.tabBar.center; 
     center.y = center.y - heightDifference/2.0 - 5 
     button.center = center; 
    } 

    self.view.addSubview(button) 
    self.view.bringSubview(toFront: button) 
} 
} 

ボタンの外観をdraw(_ rect: CGRect)機能で作成されます。

セルが選択されたときにボタンがタブバーに隠れるようにするために何かできますか?TableViewControllerにナビゲートすると正面に移動しますか?

+0

にこのボタンを追加しますか? – Poles

+0

このボタンはUIApplication.shared.delegate?.window –

答えて

2

はtabBarController.view

新しいウィンドウを作成して使用して、それのようにボタンを追加しないのはなぜ
self.tabBarController.view.addSubview(button) 
+0

に追加してください。このコードは既に 'UITabBarController'の内部にあるので、' tabBarController'プロパティはありません。 'self'はtabBarControllerです。 –

関連する問題