gpt4 book ai didi

ios - 导航栏右栏按钮项目间距

转载 作者:IT王子 更新时间:2023-10-29 05:07:47 26 4
gpt4 key购买 nike

我创建了一个 navigation bar 从 Storyboard、titleView 添加左栏按钮项,从代码添加三个右栏按钮项。

代码如下:

override func viewDidLoad() {
super.viewDidLoad()

var screenWidth = UIScreen.mainScreen().bounds.width
// custom title view
var navBarWidth: CGFloat = self.navigationController!.navigationBar.frame.size.width
let customTitleView = UIView(frame: CGRectMake(0, 0, navBarWidth, 44))
titleLabel = UILabel(frame: CGRectMake(20, 0, navBarWidth, 40))
titleLabel.text = conversationName
if let titleFont = UIFont(name: "Roboto-Regular", size: 20) {
titleLabel.font = titleFont
}
titleLabel.textColor = UIColor.whiteColor()

customTitleView.addSubview(titleLabel)
self.navigationItem.titleView = customTitleView

// right bar buttons
var searchImage = UIImage(named: "search")!
var clipImage = UIImage(named: "clip")!
var pencilImage = UIImage(named: "pencil")!

var searchBtn = UIBarButtonItem(image: searchImage, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("searchBtnPressed"))
searchBtn.tintColor = UIColor.whiteColor()
var clipBtn = UIBarButtonItem(image: clipImage, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("clipBtnPressed"))
clipBtn.tintColor = UIColor.whiteColor()
var pencilBtn = UIBarButtonItem(image: pencilImage, style: UIBarButtonItemStyle.Plain, target: self, action: Selector("pencilBtnPressed"))
pencilBtn.tintColor = UIColor.whiteColor()

self.navigationItem.setRightBarButtonItems([pencilBtn, clipBtn, searchBtn], animated: false)
}

我的问题是我想更改右键之间的间距,但我不知道如何更改。

我试图在它们之间添加一个 fixedButton,但它只是增加了现有空间。

有人能帮帮我吗?谢谢。

最佳答案

我是这样解决问题的:

var searchImage = UIImage(named: "search-selected")!
var clipImage = UIImage(named: "clip")!
var pencilImage = UIImage(named: "pencil")!

let searchBtn: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
searchBtn.setImage(searchImage, forState: UIControlState.Normal)
searchBtn.addTarget(self, action: "searchBtnPressed", forControlEvents: UIControlEvents.TouchUpInside)
searchBtn.frame = CGRectMake(0, 0, 30, 30)
let searchBarBtn = UIBarButtonItem(customView: searchBtn)

let clipBtn: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
clipBtn.setImage(clipImage, forState: UIControlState.Normal)
clipBtn.addTarget(self, action: "clipBtnPressed", forControlEvents: UIControlEvents.TouchUpInside)
clipBtn.frame = CGRectMake(0, 0, 30, 30)
let clipBarBtn = UIBarButtonItem(customView: clipBtn)

let pencilBtn: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
pencilBtn.setImage(pencilImage, forState: UIControlState.Normal)
pencilBtn.addTarget(self, action: "pencilBtnPressed", forControlEvents: UIControlEvents.TouchUpInside)
pencilBtn.frame = CGRectMake(0, 0, 30, 30)
let pencilBarBtn = UIBarButtonItem(customView: pencilBtn)

self.navigationItem.setRightBarButtonItems([pencilBarBtn, clipBarBtn, searchBarBtn], animated: false)

现在看起来不错,

Difference between before and after

Swift 4.1 更新

let testButton : UIButton = UIButton.init(type: .custom)
testButton.setImage(editImage, for: .normal)
testButton.addTarget(self, action: #selector(didTapCameraButton), for: .touchUpInside)
testButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let addButton = UIBarButtonItem(customView: testButton)

关于ios - 导航栏右栏按钮项目间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32457080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com