最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift设置导航栏返回按钮的padding

Swift crifan 4251浏览 0评论

希望都设置和头像的padding一致的10个point=20px

swift navigation bar back padding

iphone – Placing a custom view based UIBarButtonItem in the navigation bar without default horizontal padding – Stack Overflow

swift custom back button

uinavigationcontroller – Swift Custom Back Button and Destination – Stack Overflow

ios – Setting custom back button to look like default back button – Stack Overflow

然后用代码:

        let backImage = UIImage(named: "navi_back")!
        let backButton = UIButton(frame: CGRectMake(
            0,
            (HeightNaviBar – backImage.size.height) / 2,
            backImage.size.width,
            HeightNaviBar
            ))
        print("HeightNaviBar=\(HeightNaviBar)") //44.0
        print("backButton.frame=\(backButton.frame)") //(10.0, 13.5, 9.0, 44.0)
        backButton.setImage(backImage, forState: UIControlState.Normal)
        backButton.addTarget(self, action: Selector("backButtonPressed:"), forControlEvents: UIControlEvents.TouchUpInside)
       
        //for debug
        backButton.backgroundColor = UIColor.greenColor()
        let backNaviBtnItem:UIBarButtonItem = UIBarButtonItem(customView: backButton)
//        let backNaviBtnItem:UIBarButtonItem = UIBarButtonItem(
//            image: backImage,
//            style: UIBarButtonItemStyle.Plain,
//            target: self,
//            action: nil
//        )
        self.navigationItem.setLeftBarButtonItem(backNaviBtnItem, animated: true)

结果还是:

很明显:

left bar的左边,始终有个固定的padding

需要去掉

搜:

ios swift navigation bar back button left padding

ios swift back button left padding

iphone – How to Edit Empty Spaces of Left, Right UIBarButtonItem in UINavigationBar [iOS 7] – Stack Overflow

最终用:

        let backImage = UIImage(named: "navi_back")!
//        let backButton = UIButton(frame: CGRectMake(
//            0,
//            (HeightNaviBar – backImage.size.height) / 2,
//            backImage.size.width,
//            HeightNaviBar
//            ))
//        print("HeightNaviBar=\(HeightNaviBar)") //44.0
//        print("backButton.frame=\(backButton.frame)") //(10.0, 13.5, 9.0, 44.0)
//        backButton.setImage(backImage, forState: UIControlState.Normal)
//        backButton.addTarget(self, action: Selector("backButtonPressed:"), forControlEvents: UIControlEvents.TouchUpInside)
//       
//        //for debug
//        backButton.backgroundColor = UIColor.greenColor()
//
//        let backNaviBtnItem:UIBarButtonItem = UIBarButtonItem(customView: backButton)
////        let backNaviBtnItem:UIBarButtonItem = UIBarButtonItem(
////            image: backImage,
////            style: UIBarButtonItemStyle.Plain,
////            target: self,
////            action: nil
////        )
//        self.navigationItem.setLeftBarButtonItem(backNaviBtnItem, animated: true)
       
       
        let backNaviBtnItem:UIBarButtonItem = UIBarButtonItem(
            image: backImage,
            style: UIBarButtonItemStyle.Plain,
            target: self,
            action: Selector("backButtonPressed:")
        )
        let negativeLefPadding = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
        negativeLefPadding.width = –6
        self.navigationItem.setLeftBarButtonItems([negativeLefPadding, backNaviBtnItem], animated: true)

解决了问题:

转载请注明:在路上 » [已解决]swift设置导航栏返回按钮的padding

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.175 seconds, using 22.09MB memory