在导航栏中的后退按钮不能在ios 11中被轻拍

问题描述:

它工作正常,直到最近的更新,我认为导航项目应该与AutoLayout概念一起工作。我一直在使用这样的:在导航栏中的后退按钮不能在ios 11中被轻拍

let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 30, height: 30))) 
button.setImage(UIImage(named: "BackIcon"), for: UIControlState()) 
button.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0) 
button.addTarget((target != nil ? target! : self), action: backAction, for: .touchUpInside) 
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button) 

我应该做的改变,使其顺利的原因目前不被调用上,每一个水龙头,通常它采取2-3次的得到挖掘同一地区。

+0

您的#backAction代码在哪里? –

+1

我已经检查过该方法甚至没有被调用,所以发布代码将是不必要的。 – Fay007

+0

老兄我不明白吗?!如果你有一个解决方案发布它作为答案和解释。 – Fay007

重写下面的方法在我的自定义UIView类的伎俩为我:

override var intrinsicContentSize: CGSize { 
     return UILayoutFittingExpandedSize 
    } 

这对我在iOS 11上工作。退房,看看它是否适合你。

let btnLeftMenu = UIButton.init(type: .system) 
    let image = UIImage(named: "Back"); 
    btnLeftMenu.setImage(image, for: .normal) 
    btnLeftMenu.setTitle("BACK", for: .normal); 
    btnLeftMenu.imageEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0) 
    btnLeftMenu.titleEdgeInsets = UIEdgeInsetsMake(0, -30, 0, 0) 
    btnLeftMenu.sizeToFit() 
    btnLeftMenu.tintColor = UIColor.white 
    btnLeftMenu.titleLabel?.font = UIFont.init(name: "Avenir-Heavy", size: 16.0) 
    btnLeftMenu.addTarget(self, action: #selector (CustomMessageVC.backButtonAction(_:)), for: .touchUpInside) 
    let barButton = UIBarButtonItem(customView: btnLeftMenu) 
    self.navigationItem.leftBarButtonItem = barButton 

[编辑]

大概的宽度和图像高度是小的。您可以使用Xcode调试中的实时Viewstack检查后端宽度和高度。点击图片右侧的第二个按钮查看ViewStack。如果宽度和高度是问题,那么增加按钮的宽度和高度,使图像居中,使用imageView属性来适应宽高比。那应该可以解决你的问题。快乐的编码。

Image

+0

它在ios 11上工作吗?加上我看不出太大的区别! – Fay007

+0

它正在iOS 11上工作。 –

+0

修复了@ Fay007的问题? –

工作对我来说:

let backButton = UIBarButtonItem(title: "Back", 
      style: .done, 
      target: self, 
      action: #selector(moveBack)) 
navigationItem.setLeftBarButton(backButton, animated: false) 

打开查看调试和检查框架leftBarButtonItem。您应该注意leftBarButton,因为它不会从UIView继承并在运行时生成帧。

发现这个麻烦潜伏在我们的应用程序,以及现在的iOS 11出来,所以这是我的解决方案。这并不完美,因为您需要为按钮设置框架,但是可以完成这项工作,并且可以在iOS 9.3/10.3.1/11.1(可能介于两者之间)上运行。点击区域正常,用户体验良好。

前: ios11 button bounds before adjusting frame

后: tap area expanded

我称这种现象viewDidLoad

func setNavbarButtons() { 
    // setup the left and right nav bar buttons 

    // manually define the frame for the buttons 
    let buttonWidth: CGFloat = 44 
    var buttonHeight: CGFloat = buttonWidth 

    // if possible, use the nav bar height as the button height, else fall back to the manual value above 
    if let frame = self.navigationController?.navigationBar.frame { 
     buttonHeight = frame.height 
    } 

    // apply this to the left inset for the left button, right inset for the right button, so the image is pushed to the left/right respectively 
    // (hence the negative value) 
    // setting this to 0 will center the image in the button and we don't want that 
    let edgeInset = -(buttonWidth/2) 

    // setup a button to hold the image (left) 
    let leftButton = UIButton(type: .custom) 
    let backIcon = UIImage(named: "Back with shadow") 
    backIcon!.isAccessibilityElement = true 
    backIcon!.accessibilityLabel = "Back" 

    // no title text 
    leftButton.setTitle("", for: .normal) 
    leftButton.setImage(backIcon, for: .normal) 
    leftButton.sizeToFit() 
    leftButton.addTarget(self, action: #selector(self.didTapLeftNavbarButton), for: .touchUpInside) 

    // define left button frame and inset 
    leftButton.frame.size.width = buttonWidth 
    leftButton.frame.size.height = buttonHeight 
    leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, edgeInset, 0, 0) 

    // finally setup a UIBarButtonItem to hold the UIButton (arg) 
    let leftBarButtonItem = UIBarButtonItem(customView: leftButton) 
    leftBarButtonItem.title = "" 

    // set it 
    self.navigationItem.setLeftBarButton(leftBarButtonItem, animated: true) 

    // rinse/wash/repeat for right button 
    let rightButton = UIButton(type: .custom) 
    let shareIcon = UIImage(named: "Share") 
    shareIcon!.isAccessibilityElement = true 
    shareIcon!.accessibilityLabel = "Share" 

    // no title text 
    rightButton.setTitle("", for: .normal) 
    rightButton.setImage(shareIcon, for: .normal) 
    rightButton.sizeToFit() 
    rightButton.addTarget(self, action: #selector(self.didTapActionButton(_:)), for: .touchUpInside) 

    // define right button frame and inset 
    rightButton.frame.size.width = buttonWidth 
    rightButton.frame.size.height = buttonHeight 
    rightButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, edgeInset) 

    let rightBarButtonItem = UIBarButtonItem(customView: rightButton) 
    rightBarButtonItem.title = "" 

    self.navigationItem.setRightBarButton(rightBarButtonItem, animated: true) 
}