如何将导航按钮一直约束到左侧或右侧? (iOS)

时间:2018-10-17 14:56:59

标签: ios user-interface uinavigationbar

我试图将导航栏按钮一直向右和向左对齐,因为屏幕边缘和按钮之间有一定的间隔。有什么想法可以将按钮的前导锚限制在导航栏/屏幕的前边缘吗?

1 个答案:

答案 0 :(得分:1)

添加导航项时,您无需设置约束,而只需设置框架。

我遇到了您的问题,当前在iOS 11上遇到过。我使用的一种快速解决方案是添加一个清晰的负间隔条按钮。像这样:

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
}

我希望这会有所帮助。