iOS development encounters a custom navigation bar Leftbarbuttonitems causes sliding return failure problem resolution

Source: Internet
Author: User
Tags uikit

When you use the navigation controller Navigationcontroller to jump to another page, you can return to the previous layer by sliding to the left of the left corner of the screen, except by clicking the return button on the left-hand side. However, if you customize the Self.navigationItem.leftBarButtonItems, you will find that the slide return (swipe back) is invalid.
(How to customize the navigation bar to the left button to see this article: Swift-Modify the navigation bar "back" button text, icon)


1, let slide return continue valid

The solution is to let Viewcontroller implement the Uigesturerecognizerdelegate protocol.


Import Uikit

Class Detailviewcontroller:uiviewcontroller, Uigesturerecognizerdelegate {

Override Func Viewdidload () {
Self.title = "Hangge.com"
Let button = UIButton (type:. System)
Button.frame = CGRectMake (0, 0, 65, 30)
Button.setimage (UIImage (named: "Back"), Forstate:. Normal)
Button.settitle ("Back", Forstate:.) Normal)
Button.addtarget (Self, Action: "Backtoprevious", forControlEvents:. Touchupinside)

Let leftbarbtn = Uibarbuttonitem (Customview:button)

To remove the left space, or the button is not on top of the front
Let spacer = Uibarbuttonitem (barbuttonsystemitem:. Fixedspace, Target:nil,
Action:nil)
Spacer.width =-10;

Self.navigationItem.leftBarButtonItems = [SPACER,LEFTBARBTN]

Enable slide return (swipe back)
Self.navigationcontroller? interactivepopgesturerecognizer!. delegate = Self
}

Return button click Response
Func backtoprevious () {
Self.navigationcontroller? Popviewcontrolleranimated (True)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Note: Enabling sliding return (swipe back) is valid for all Viewcontroller that are currently Navigationcontroller managed. You don't need every viewcontroller to call that method, we just make sure they're in the same uinavigationcontroller.

2, with webview gesture conflict caused unable to slide back

Normally, you can slide back through the above settings, but sometimes we put a webview in the page and load the Web page, and then we find that the function of the slide return fails. (WebView does not have this problem if the page is not loaded)

Problem reason: Because the webview load of the page itself need to use the gesture operation, or webview enlarged after the need for some sliding view operation, thus causing the incident conflict.

Workaround: Create a tap gesture, set up a proxy, and implement a proxy method that allows multiple gestures concurrency


Import Uikit

Class Detailviewcontroller:uiviewcontroller, Uigesturerecognizerdelegate {

@IBOutlet weak var webview:uiwebview!

Override Func Viewdidload () {
Self.title = "Hangge.com"

Let Urlobj = Nsurl (string: "http://www.hangge.com")
Let request = Nsurlrequest (url:urlobj!)
Webview.loadrequest (Request);

Let button = UIButton (type:. System)
Button.frame = CGRectMake (0, 0, 65, 30)
Button.setimage (UIImage (named: "Back"), Forstate:. Normal)
Button.settitle ("Back", Forstate:.) Normal)
Button.addtarget (Self, Action: "Backtoprevious", forControlEvents:. Touchupinside)
Let leftbarbtn = Uibarbuttonitem (Customview:button)

To remove the left space, or the button is not on top of the front
Let spacer = Uibarbuttonitem (barbuttonsystemitem:. Fixedspace, Target:nil,
Action:nil)
Spacer.width =-10;

Self.navigationItem.leftBarButtonItems = [SPACER,LEFTBARBTN]

Enable slide return (swipe back)
Self.navigationcontroller? interactivepopgesturerecognizer!. delegate = Self

Create a new sliding gesture
Let tap = Uiswipegesturerecognizer (target:self, Action:nil)
Tap.delegate = Self
Self.webView.addGestureRecognizer (TAP)
}

Returns true to indicate that all gesture recognition of the same type will be handled
Func Gesturerecognizer (Gesturerecognizer:uigesturerecognizer,
Shouldrecognizesimultaneouslywithgesturerecognizer Othergesturerecognizer:
Uigesturerecognizer)-> Bool {
return True
}

Return button click Response
Func backtoprevious () {
Self.navigationcontroller? Popviewcontrolleranimated (True)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Original: www.hangge.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.