ios - LeftBarButtonItem 位置不正确

标签 ios swift button uiimage uibarbuttonitem

我正在尝试像这样添加一个 leftbarbuttonitem: enter image description here

使用此代码:

let button = UIButton.init(type: .custom)
        button.setImage(UIImage.init(named: "WhiteBackButton.png"), for: UIControlState.normal)
        button.addTarget(self, action:#selector(self.backButtonSelected), for: UIControlEvents.touchUpInside)
        button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
        let barButton = UIBarButtonItem.init(customView: button)
        self.navigationItem.leftBarButtonItem = barButton

我正在使用这张图片: enter image description here

它看起来像这样被推过去:enter image description here

如何阻止它从左侧推得太远?

最佳答案

let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
        negativeSpacer.width = -10
        self.navigationItem.leftBarButtonItems = [negativeSpacer, leftBarButton]

关于ios - LeftBarButtonItem 位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43770007/

相关文章:

iOS8.1收不到苹果推送

ios - 从 Storyboard 场景停靠项中实例化新 View

iphone - SVprogressHUD 掩码没有填满整个 View

javascript - 在按钮中使用 math.random 来设置选择选项?

css - 禁用和 css 样式按钮中的文本显示 IE 中的移动

ios - Obj-c - 返回错误的键盘高度?

ios - AVKit – 视频剪辑仅循环 2 次

ios - SwiftUI MVVM : child view model re-initialized when parent view updated

ios - 在不同的 UIKit 和 SpriteKit 场景中连续播放音频

python - 如何从 PyGTK 中的主题设置图标