第一个leftBarButtonItem隐藏

时间:2018-07-13 05:55:06

标签: ios swift uinavigationitem

我使用了2个条形按钮项目。一个用于后退按钮,第二个用于标题。一切正常。但是,当标题字符串很大时,标题将向左移动,并且后退按钮不会出现,但可以使用。

我还使用了rightBarButtonItem,该徽章带有徽章按钮。但这并不影响此操作,因为在删除该按钮后我尝试了相同的代码。这是我的代码-

let backBtnImg: UIImage = UIImage(named: "Back Image")!
let Back: UIBarButtonItem = UIBarButtonItem(image: backBtnImg, style: .plain, target: self, action: #selector(backButtonAction))

let titleStr = ("titleString")
let titleItem: UIBarButtonItem = UIBarButtonItem(title: titleStr, style: .plain, target: nil, action: nil)
self.navigationItem.leftBarButtonItems = [Back, titleItem]

我已经附上了两张图片。

When title is small

When title is large

3 个答案:

答案 0 :(得分:0)

使用此One It可以帮助您

->仅需向左一个按钮

->对于标题,请使用:self.navigationItem.title = "Navigation Title"

 let backBtnImg: UIImage = UIImage(named: "back")!
        let Back: UIBarButtonItem = UIBarButtonItem(image: backBtnImg, style: .plain, target: self, action: #selector(backButtonAction))
        self.navigationItem.title = "Navigation Title"
        self.navigationItem.leftBarButtonItems = [Back]

答案 1 :(得分:0)

如果要在左侧设置标题,请尝试此代码。这可能对您有帮助。

viewDidLoad方法中

let navView = UIView(frame: CGRect(x: 0, y: 0, width: (self.navigationController?.navigationBar.frame.size.width)! - 50, height: 40))
    lblTitle = UILabel(frame: CGRect(x: 0, y: 0, width: navView.frame.size.width - 40, height: 40))
    lblTitle?.text = strTitle
    lblTitle?.backgroundColor = UIColor.clear
    lblTitle?.textColor = UIColor.white
    lblTitle?.textAlignment = .left
    navView.addSubview(lblTitle!)
    self.navigationItem.titleView = navView

答案 2 :(得分:0)

func setupNevigationBar(){
            let btnBack = UIButton(type: .custom)
            btnBack.setImage(#imageLiteral(resourceName: "back"), for: .normal)
            btnBack.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
            btnBack.addTarget(self, action: #selector(btnBackPressed), for: .touchUpInside)
            let itemBack = UIBarButtonItem(customView: btnBack)

            let titleStr = ("Regular Title")
            let titleItem: UIBarButtonItem = UIBarButtonItem(title: titleStr, style: .plain, target: nil, action: nil)
            self.navigationItem.leftBarButtonItems = [itemBack, titleItem]

            self.navigationItem.setLeftBarButton(itemBack, animated: true)
        }

enter image description here

enter image description here