2016-12-22 3 views
1

Я подклассифицирую UITabBarController, и я пытаюсь добавить кнопку в центр панели вкладок. Это показывает красиво на первый:Держите UIButton впереди в TabBar

enter image description here

Затем, когда я выбираю пункт в моем TableViewController, он остается видимым:

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

Yry, чтобы добавить эту кнопку в UIApplication.shared.delegate? .window –

ответ

2

Добавить эту кнопку tabBarController.view

self.tabBarController.view.addSubview(button) 
+0

это не сработало, потому что этот код уже внутри 'UITabBarController', поэтому нет свойства' tabBarController' - 'self' является tabBarController. –

Смежные вопросы