Change position of UIBarButtonItem in UINavigationBar

后端 未结 20 1562
一个人的身影
一个人的身影 2020-11-27 10:31

How can I change the position of a UIBarButtonItem in a UINavigationBar? I would like my button to be about 5px higher than its normal position.

20条回答
  •  没有蜡笔的小新
    2020-11-27 10:57

    I needed to set my button to be more over towards the right. Here's how I did it using UIAppearance in Swift. There's a vertical position property there as well so I imagine you can adjust in any direction.

    UIBarButtonItem.appearance().setTitlePositionAdjustment(UIOffset.init(horizontal: 15, vertical: 0), forBarMetrics: UIBarMetrics.Default)
    

    This seems much less invasive to me than messing with the frame directly or adding custom subviews.

提交回复
热议问题