2017-10-05 53 views
0

它工作正常,直到最近的更新,我認爲導航項目應該與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

回答

0

重寫下面的方法在我的自定義UIView類的伎倆爲我:

override var intrinsicContentSize: CGSize { 
     return UILayoutFittingExpandedSize 
    } 
0

這對我在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的問題? –

0

工作對我來說:

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

打開查看調試和檢查框架leftBarButtonItem。您應該注意leftBarButton,因爲它不會從UIView繼承並在運行時生成幀。

0

發現這個麻煩潛伏在我們的應用程序,以及現在的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) 
}