I would like to add a cycle view and a label to UINavigation. like this:
I can set a label to my UINavigation by this code:
if let navigationBar = self.navigationController?.navigationBar {
let firstFrame = CGRect(x: 300, y: 0, width: navigationBar.frame.width/2, height: navigationBar.frame.height)
let firstLabel = UILabel(frame: firstFrame)
firstLabel.text = "First"
navigationBar.addSubview(firstLabel)
}
but I have two problems by this code:
1.how to set x
position correctly?
(to test I set 300 value, but this value show different position on different screen sizes)
2. how to set a cycle background to this label ?
You can add both of the view (red circle) and the label (number 16) programmatically as a subView to the button of the bar button item.
What you should do is:
Make sure that the connected component is the
UIButton
, but NOTUIBarButtonItem
.As you can see, I called it
btnMenu
.btnMenu
:It should be similar to:
And that's it!
UIButton is a subclass of UIView, meaning that you can add subviews to it (addSubview(_:)).
Output:
Hope this helped.