简体   繁体   English

导航栏右栏按钮项间距

[英]navigation bar right bar button items spacing

I have created a navigation bar http://oi61.tinypic.com/r0p8w4.jpg with left bar button item added from storyboard, titleView and three right bar button items from code. 我创建了一个导航栏http://oi61.tinypic.com/r0p8w4.jpg ,从故事板,titleView和代码中的三个右键按钮项添加了左栏按钮项。

Here is the code: 这是代码:

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)
}

My problem is that I want to change the spacing between right buttons but I don't know how. 我的问题是我想改变右键之间的间距,但我不知道如何。

I've tried to add a fixedButton between them but it just increased the existing space. 我试图在它们之间添加一个fixedButton,但它只是增加了现有的空间。

Can some one help me? 有人能帮我吗? Thanks. 谢谢。

I solved my problem in this way: 我用这种方式解决了我的问题:

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)

Now it looks good, 现在看起来不错,

之前和之后的区别

Update for Swift 4.1 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)

For Swift 3: 对于Swift 3:

let searchBtn: UIButton = UIButton(type: UIButtonType.custom)
searchBtn.setImage(UIImage(named: "search"), for: [])
searchBtn.addTarget(self, action: #selector(ViewController.searchBtnPressed(_:)), for: UIControlEvents.touchUpInside)
searchBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let searchButton = UIBarButtonItem(customView: searchBtn)

let clipBtn: UIButton = UIButton(type: UIButtonType.custom)
clipBtn.setImage(UIImage(named: "clip"), for: [])
clipBtn.addTarget(self, action: #selector(ViewController.clipBtnPressed(_:)), for: UIControlEvents.touchUpInside)
clipBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let clipButton = UIBarButtonItem(customView: clipBtn)

let pencilBtn: UIButton = UIButton(type: UIButtonType.custom)
pencilBtn.setImage(UIImage(named: "pencil"), for: [])
pencilBtn.addTarget(self, action: #selector(ViewController.pencilBtnPressed(_:)), for: UIControlEvents.touchUpInside)
pencilBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let pencilButton = UIBarButtonItem(customView: pencilBtn)

self.navigationItem.rightBarButtonItems = [pencilButton, clipButton, searchButton]

Replace ViewController with your view controller ViewController替换为视图控制器

Set testButton.frame doesn't help. 设置testButton.frame没有帮助。

This solution is correct for me! 这个解决方案对我来说是对的!

rightButton.imageEdgeInsets = UIEdgeInsets(top: 3, left: 10, bottom: 7, right: 0)

This solution is in Objective C 该解决方案在Objective C中

UIImage *settingImageName = [UIImage imageNamed:@"Menu_Burger_Icon"];
UIButton * settingButton = [UIButton buttonWithType:UIButtonTypeCustom];
[settingButton setImage:settingImageName forState:UIControlStateNormal];
[settingButton addTarget:self action:@selector(settingsBtnClicked) forControlEvents:UIControlEventTouchUpInside];
settingButton.frame = CGRectMake(0, 0, 30, 30);

UIBarButtonItem *settingBarButton = [[UIBarButtonItem alloc] initWithCustomView:settingButton];

UIImage *notificationImageName = [UIImage imageNamed:@"NotificationON"];
UIButton * notificationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[notificationButton setImage:notificationImageName forState:UIControlStateNormal];
[notificationButton addTarget:self action:@selector(notificationButtonClicked) forControlEvents:UIControlEventTouchUpInside];
notificationButton.frame = CGRectMake(0, 0, 30, 30);
UIBarButtonItem *notificationBarButton = [[UIBarButtonItem alloc] initWithCustomView:notificationButton];

self.navigationItem.rightBarButtonItems  = @[settingBarButton,notificationBarButton];

Solution reference by @mikle94. @ mikle94的解决方案参考。 His answer is in Swift. 他的回答是在斯威夫特。

let rightActionButton:UIButton = UIButton()

if #available(iOS 11.0, *) {

    let widthConstraint = rightActionButton.widthAnchor.constraint(equalToConstant: 30)
    let heightConstraint = rightActionButton.heightAnchor.constraint(equalToConstant: 30)
    heightConstraint.isActive = true
    widthConstraint.isActive = true
}
else {
    rightActionButton.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30)
}

rightActionButton.setImage(UIImage.init(named: "3-dots-right-button"), for: .normal)
rightActionButton.addTarget(self, action: #selector(handleRightMenu), for: UIControlEvents.touchUpInside)
rightActionButton.setTitle("", for: .normal)
rightActionButton.tintColor = UIColor(hex:K.NODD_GREEN_HEX)


let rightActionBarButton:UIBarButtonItem = UIBarButtonItem(customView: rightActionButton)

let rightBarButtonItem1 = rightActionBarButton
let rightBarButtonItem2 = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(handleRight2Menu))

self.navigationItem.rightBarButtonItems = [rightBarButtonItem1,rightBarButtonItem2]
// create three nav bar buttons
    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()

// create a spacer
  var space = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: self, action: nil)
  space.width = 10

  var buttons = [pencilBtn, space, clipBtn, space, searchBtn]
  navigationItem?.rightBarButtonItems = buttons

Try to change the constraintEqualToConstant 尝试更改constraintEqualToConstant

[myUIButton.widthAnchor constraintEqualToConstant:24].active = YES;
[myUIButton.heightAnchor constraintEqualToConstant:24].active = YES;

You can also wire things up first in Storyboard. 您还可以在Storyboard中首先连接。
I have two right bar button items. 我有两个正确的栏按钮项目。

(This is objective-c code, which you can modify for swift.) (这是Objective-c代码,您可以为swift修改它。)

First create references to them in view controller. 首先在视图控制器中创建对它们的引用。

@interface MyViewController ()
    @property (weak, nonatomic) IBOutlet UIButton *smsButton;
    @property (weak, nonatomic) IBOutlet UIButton *checkoutButton;
@end

Then, in viewDidLoad, adjust their widths and heights. 然后,在viewDidLoad中,调整它们的宽度和高度。

// Adjust right bar button item spacing.
self.smsButton.frame = CGRectMake(0, 0, 30, 30);
self.lockButton.frame = CGRectMake(0, 0, 30, 30);

Supporting solution by @mkz, by using function to reduce the code (Swift 4.2.1) @mkz支持解决方案,通过使用函数来减少代码(Swift 4.2.1)

Added most important parameters to the function (note how to pass selector to a function), you may want to add more as per your need. 为函数添加了最重要的参数(注意如何将选择器传递给函数),您可能希望根据需要添加更多参数。

func makeCustomNavigationButton(imageName: String, action: Selector) -> UIBarButtonItem{

    let image = UIImage(named: imageName)!
    let btn: UIButton = UIButton(type: UIButton.ButtonType.custom)
    btn.setImage(image, for: .normal)
    btn.addTarget(self, action: action, for: .touchUpInside)
    btn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    let barBtn = UIBarButtonItem(customView: btn)

    return barBtn
}

How to call: 怎么称呼:

let search = self.makeCustomNavigationButton(imageName: "search", action: #selector(searchBtnPressed(_:)))
let clip = self.makeCustomNavigationButton(imageName: "clip", action: #selector(clipBtnPressed(_:)))
let pencil = self.makeCustomNavigationButton(imageName: "pencil", action: #selector(pencilBtnPressed(_:)))
self.navigationItem.rightBarButtonItems = [search, clip, pencil]

This is one of methods to solve that. 这是解决这个问题的方法之一。

Use UIBarButtonItem as a space 使用UIBarButtonItem作为空格

let space = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
space.width = -20 // adjust as needed
self.navigationItem.rightBarButtonItems = [pencilBtn, clipBtn, searchBtn, space]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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