如何更改导航栏中的按钮位置

How to change button position in navigation bar

所以我在导航栏中创建了一个按钮并为其设置了一个图像。这是故事板中的样子:http://imgur.com/cSRfHs3

所以我试着将它设置在屏幕的边缘,这样图像和屏幕的左侧之间就没有缝隙了。

这是我使用的代码:

var screenWidth: CGFloat=0.0
    var screenHeight: CGFloat=0.0


    @IBOutlet weak var button1: 

    override func viewDidLoad() {
    super.viewDidLoad()
    UIButton!        var screenSize: CGRect = UIScreen.mainScreen().bounds
    screenWidth = screenSize.width
    screenHeight = screenSize.height

    button1.frame.size = CGSizeMake(133, 44)
    button1.frame.origin = CGPoint(x: screenWidth-(button1.frame.width+20), y: 20)
    self.view.addSubview(button1)

结果如下:http://imgur.com/HZqxnqr

发生这种情况是因为我将按钮直接添加到 TableView 吗?

您需要将按钮直接放在导航栏中。您也可以使用 xcode 故事板图形用户界面向您的按钮添加约束。