How to adjust space between two UIBarButtonItem in rightBarButtonItems

前端 未结 14 2096
悲&欢浪女
悲&欢浪女 2020-12-08 06:44

I am using the following codes to add two button to self.navigationItem.rightBarButtonItems, and I think in iOS7, the space between two buttons are too wide, is there a way

14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 07:21

    My solution is using a custom view for right bar buttons. Create a horizontal stackview with equal spacing and add any number of buttons as subview.

    Sample code:

    func addRightBarButtonItems()
    {
        let btnSearch = UIButton.init(type: .custom)
        btnSearch.setImage(UIImage(named: "icon-search"), for: .normal)
        btnSearch.addTarget(self, action: #selector(MyPageContainerViewController.searchButtonPressed), for: .touchUpInside)
    
        let btnEdit = UIButton.init(type: .custom)
        btnEdit.setImage(UIImage(named: "icon-edit"), for: .normal)
        btnEdit.addTarget(self, action: #selector(MyPageContainerViewController.editButtonPressed), for: .touchUpInside)
    
        let stackview = UIStackView.init(arrangedSubviews: [btnEdit, btnSearch])
        stackview.distribution = .equalSpacing
        stackview.axis = .horizontal
        stackview.alignment = .center
        stackview.spacing = 8
    
        let rightBarButton = UIBarButtonItem(customView: stackview)
        self.navigationItem.rightBarButtonItem = rightBarButton
    }
    

提交回复
热议问题