简体   繁体   中英

How to constrain navigation buttons all the way to the left or right? (iOS)

I am trying to align the navigation bar buttons all the way to the right and left because there is some space between the edge of the screen and the buttons. Any ideas how to constrain the leading anchor of the button to the leading edge of the navigation bar/screen?

When adding navigation items, you do not need to set constraints, but rather, frames.

I've experienced your issue and currently experiencing it on iOS 11. A quick solution that I use is to add a clear negative spacer bar button. Like so:

internal lazy var button_Skip: UIButton = {
    let button = UIButton(type: .custom)
    button.setup("SKIP".localized(), normalTextColor: .blue)
    button.isHidden = true
    return button
}()

let barButton = UIBarButtonItem(customView: self.button_Skip)
self.button_Skip.frame = CGRect(x: 0, y: 0, width: 55.0, height: 44.0)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target: nil, action: nil)
if #available(iOS 11.0, *) {
    negativeSpacer.width = -10.0
}

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM