Reputation: 1496
I have a UISplitViewController with a MasterViewController and a DetailViewController.
In my MasterViewController i am adding a button with an target action assigned and it works all good... BUT:
I want my button to be centered on the border between MasterViewController and DetailViewController. But half of the button simply disappears.. i already experimented with zPosition in layer etc.. but it didnt work.
Could anybody help me out here?
Upvotes: 0
Views: 174
Reputation: 793
You mean: A button on top of the border line which separates Master View and Detail View right?
Then you can add a button to the window directly from any of the Master or Detail controllers. (Am not sure whether this is a best practise though.)
You need to consider three things here:
This will work when you add button from viewDidLayoutSubviews(). It was not working from viewDidLoad() or viewWillAppear().
As the button is being added from viewDidLayoutSubviews(), there are chances where this method might be called multiple times having button to be created multiple times and leaving the earlier button instances leaked. So you might think of having a class instance for button some where deepening on your design of the app.
Act on the position(or state) of the button when the device rotates.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
/// Have a precise logic to centre it
let rect = CGRect(x: 270, y: 200, width: 100, height: 30)
let button = UIButton(frame: rect)
button.setTitle("Test Button", for: .normal)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(MasterViewController.testButtonTapped(sender:)), for: .touchUpInside)
let window = self.view.window
window?.addSubview(button)
window?.bringSubview(toFront: button)
}
func testButtonTapped(sender: UIButton) {
debugPrint("Test button tapped")
}
Upvotes: 1