美文网首页
【ios开发】导航栏添加左右按钮出现右移的解决方案

【ios开发】导航栏添加左右按钮出现右移的解决方案

作者: WMSmile | 来源:发表于2018-06-22 16:12 被阅读162次

    解决导航栏,添加按钮,布局右移的问题

      let spaceButtonItem = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: self, action: nil);
            spaceButtonItem.width = -15;
            
            
            let leftBtn = UIButton.init(type: .custom);
            leftBtn.setTitle("返回", for: .normal);
            leftBtn.setImage(UIImage.init(named: "icon_back"), for: .normal);
            leftBtn.setTitleColor(UIColor.black, for: .normal);
            leftBtn.frame = CGRect(x: -20, y: 0, width: 60, height: 44)
            let leftItem = UIBarButtonItem.init(customView: leftBtn);
            leftItem.width = -15;
            leftItem.style = UIBarButtonItemStyle.plain;
            leftBtn.addTarget(self, action: #selector(gobackVC(sender:)), for: .touchUpInside);
    //        self.navigationItem.leftBarButtonItem = leftItem
            self.navigationItem.leftBarButtonItems = [spaceButtonItem,leftItem];
           
            let spaceButtonItem1 = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: self, action: nil);
            spaceButtonItem1.width = -15;
            
            let rightBTN = UIButton.init(type: .custom);
            rightBTN.setTitle("关闭", for: .normal);
            rightBTN.setTitleColor(UIColor.black, for: .normal);
    //        rightBTN.setImage(UIImage.init(named: "iconz-add_photo"), for: .normal);
            rightBTN.imageView?.contentMode = .center;
            rightBTN.frame = CGRect(x: 20, y: 0, width: 60, height: 40)
            rightBTN.addTarget(self, action: #selector(dismissVC(sender:)), for: .touchUpInside);
            let rightItem = UIBarButtonItem.init(customView: rightBTN);
            self.navigationItem.rightBarButtonItems = [spaceButtonItem1,rightItem];
            
    

    注:leftItem.width = -15; 可以根据具体情况调整

    相关文章

      网友评论

          本文标题:【ios开发】导航栏添加左右按钮出现右移的解决方案

          本文链接:https://www.haomeiwen.com/subject/bngkyftx.html