热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

导航栏中的后退按钮未在ios11中轻触-Backbuttoninnavigationbarnotgettingtappedinios11

ItwasworkingfineuntiltherecentupdatewhereithinkthenavigationitemshouldworkwiththeA

It was working fine until the recent update where i think the navigation item should work with the AutoLayout concept. I have been using it like this:

它工作正常,直到最近的更新,我认为导航项应该与AutoLayout概念一起使用。我一直在使用它:

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)

what changes i should do to make it smooth cause at the moment it doesn't gets called on every tap, usually it's taking 2-3 times to get tapped in the same region.

我应该做些什么改变才能使它平滑,因为它不会在每次敲击时被调用,通常需要2-3次才能在同一区域内进行调整。

4 个解决方案

#1


0  

This worked for me on iOS 11. Check out and see if it works for you.

这在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

[EDIT]

Probably the width and height of the image are small. You can check the backbutton width and height using realtime Viewstack on Xcode debug. Click on 2nd button from right in the image to see the ViewStack. if width and height are the issues then increase the button width and height with image being centred and imageView property to aspect fit. that should solve your problem. Happy coding.

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

Image

#2


0  

Works for me:

适合我:

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

Open view debugger and check frame for leftBarButtonItem. You should be careful with leftBarButton, because it doesn't inherit from UIView and generate frame in runtime.

打开视图调试器并检查leftBarButtonItem的框架。你应该小心使用leftBarButton,因为它不会从UIView继承并在运行时生成帧。

#3


0  

Found this trouble lurking in our app as well now that iOS 11 is out, so here's my solution. It's not perfect as you do need to set the frame for the buttons, but gets the job done, and works on iOS 9.3/10.3.1/11.1 (and probably in between). Tap area is normal and UX is good.

现在iOS 11已经出现,我们的应用程序中发现了这个问题,所以这是我的解决方案。它并不完美,因为你需要设置按钮的框架,但完成工作,并在iOS 9.3 / 10.3.1 / 11.1(可能在其间)工作。 Tap区域正常,UX很好。

Before: ios11 button bounds before adjusting frame

After: tap area expanded

I call this in viewDidLoad:

我在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)
}

#4


0  

Overriding the following method in my custom UIView class did the trick for me:

在我的自定义UIView类中覆盖以下方法对我来说是个窍门:

override var intrinsicContentSize: CGSize {
        return UILayoutFittingExpandedSize
    }

推荐阅读
author-avatar
无奈的双子星_403
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有