最新消息:20190529 VPS服务器已从腾讯云香港换为Vultr新加坡,主题仍用朋友推荐的大前端D8

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

Swift crifan 1163浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
58 queries in 0.116 seconds, using 18.64MB memory