1 lines of code custom "TabBar"-B for each controller

Source: Internet
Author: User

This article will generally take you to achieve the following functions, nonsense less, first look at things:


What's in the Jpnavigationcontroller.gifq&a:demo? 01. About Customizing the navigation bar
    • 01, the first controller's navigation bar is transparent, the second controller's navigation bar is white, the third controller's navigation bar is orange.
      So, customize your own navigation bar for each controller.
    • 02, support full-screen right slide, this is absolutely necessary. On the full screen right slide, the most detailed, but also the first to explore the problem, I learned that j_ rain, he should be full-screen right-slip originator.
    • 03, the most important point, requires full-screen right-slide back when the navigation bar to follow their own controller smooth sliding.
    • 04, the following belongs to the updated content, for full-screen right-slide pop gestures to add a sliding start point control API. That is, you can precisely control how long it is right to slide from the left side of the screen, and pop gestures are effective.
    • 05, some students and I said that they need to temporarily turn off pop gestures in some interfaces, let me handle it. Now that it's done, you can temporarily turn off the pop gesture and turn it on again when you need it.
02, this new add custom Tabbar

Existing issues

    • Some students do shopping software, you may need to hover at the bottom of a controller screen a "buy Now", "consulting immediately" and other controls. Experienced students encounter this type of function may be used to create a new window, and then add the control they need to the window, and then set the window hidden = NO, it can be achieved. Indeed, in addition to creating a new window, you can also add your own controls to the current Keywindow, as well as implement this function.
    • This feature is implemented, but one thing that's disgusting is that Apple has had a right-swipe gesture since iOS7 began. This time, compared to the Android forward Back button is a single in the window display a controller, the right swipe gesture allows the window to display simultaneously two controller view.
    • If you follow the above to add a window or add a keywindow on the way. First, you can't let your definition of this control slide right as the user slides right. Second, you can only control the display and hiding of your controls in these two methods. But when you try it, you'll find that when the user slides right, your control jumps and flashes. But we are having the pursuit of the program dog, and we obviously will say:fuck on this scheme away! This was so UGLY man. and IT smells like shit.
      -(void)viewWillAppear:(BOOL)animated-(void)viewWillDisappear:(BOOL)animated

Solution Solutions

    • This time I combined the previous custom navigation bar to give a more "elegant" solution:
    • If a is the root controller, B is the next controller to push past, you only need to write the following line after the A in the push method. You can have a "linkage" Tabbar with the navigation bar that follows the user's right-swipe, and you don't have to care about the Tabbar's display and hide.
      [self.navigationController pushViewController:YourOwnVC animated:YES];YourOwnVC.navigationController.jp_linkViewHeight = YourHopeHeight;
01. What did the article say before?

99% of the students who saw my article did not read my previous article, so I need to re-describe the contents of the previous article. But if you want to learn more about the implementation of ideas or need to look back to my article, because this article I speak Tabbar, so Navgationcontroller will not speak very thin.
The previous article was broadly clear, how to adapt to the need to customize the navigation bar for each interface needs.
The following ideas are implemented:

    • When we call this Initwithrootviewcontroller construction method, we first use a jpnavigationcontroller as the root controller, which is responsible for all push and pop operations.
      [[JPNavigationController alloc]initWithRootViewController:vc];
    • Then, when the user invokes the Push method, the controller a passed in by the user first is wrapped up in a jpwarpnavigationcontroller (inheritance and Uinavigationcontroller) to be B, The b is then packaged with a jpwarpviewcontroller as C and then jpnavigationcontroller with the root navigation controller to push and pop.
    • After this packaging, each controller has its own navigation bar, so the user can customize each navigation bar, such as transparency, color, gradient and other operations.
    • The reason for these two tiers of packaging is that Apple's default rule is that the navigation controller cannot push and pop navigation controllers.
02, navigation bar Update part of the implementation of ideas?

To achieve the goal: 1. Temporarily turn off pop gestures 2. Customizing the right swipe gesture active area

  • The Interactivepopgesturerecognizer of the system only provides an enabled attribute to us, so if you want to do more things you need to think of other ways. In this case, I chose to customize a uipangesturerecognizer to replace the system's gesture implementation

    Custom Pop gestures-(Uipangesturerecognizer *) jp_fullscreenpopgesturerecognizer{if (!_jp_fullscreenpopgesturerecognizer) {_jp_fullscreenpopgesturerecognizer = [Uipangesturerecognizer new]; _jp_fullscreenpopgesturerecognizer. maximumnumberoftouches =1; }return _jp_fullscreenpopgesturerecognizer;} -(void) viewdidload{[Super Viewdidload];Hide the navigation bar completely [Self Setnavigationbarhidden:YES];Adding pop gestures (lazy loading)if (![Self. Interactivepopgesturerecognizer. View. Gesturerecognizers Containsobject:Self. Jp_fullscreenpopgesturerecognizer]) {[Self. Interactivepopgesturerecognizer. View Addgesturerecognizer:Self. Jp_fullscreenpopgesturerecognizer];Replace the system's pops with your own gesturesNsarray *targets = [Self. Interactivepopgesturerecognizer Valueforkey: @ "targets"]; id target = [Targets.firstobject valueforkey:@" target "]; SEL action = nsselectorfromstring (@ " Handlenavigationtransition: "); self.jp_fullscreenpopgesturerecognizer .delegate = [self jp_popgesturerecognizerdelegate]; [self.jp_fullscreenpopgesturerecognizer addTarget:target Action:action]; //system gesture is not available self.enabled = NO;}}   
  • This allows us to use our own gestures instead of the system's right-slide function, so we can customize the right-slip feature in a custom gesture proxy method:

    -(BOOL) Gesturerecognizershouldbegin: (Uipangesturerecognizer *) gesturerecognizer{The root controller does not allow popif (Self. Navigationcontroller. viewcontrollers. Count <=1) {ReturnNO; }Pop is not allowed when the user is forbiddenif (!Self. Navigationcontroller. jp_fullscreenpopgestureenabled) {ReturnNO; }Suppresses pop when the point at which the trigger starts is greater than the maximum trigger point specified by the userCgpoint beginninglocation = [Gesturerecognizer locationinview:gesturerecognizer. view];CGFloat maxallowedinitialdistance =Self. Navigationcontroller. Jp_interactivepopmaxallowedinitialdistancetoleftedge;if (maxallowedinitialdistance >=0 && Beginninglocation. x > Maxallowedinitialdistance) {ReturnNO; }Suppress pop when transitioning animations are in progressif ([[Self.navigationcontroller valueforkey:@ "_isTransitioning"] Boolvalue]) {return NO;} //reverse slide prohibit pop cgpoint translation = [Gesturerecognizer Translationinview:gesturerecognizer.view]; if (Translation.x <= 0) {return NO;} //temporarily turn off pop gestures prohibit pop if (self.closepopfortemporary) {return NO;} Span class= "Hljs-keyword" >return yes;}         
03, Custom Tabbar?
  • Custom Tabbar should meet two points: 1. Slide with the user sliding smoothly 2. You do not need to handle tabbar display and hide in Viewwillappear and viewwilldisappear.
  • To meet the above two points, the first thing to consider is that our Tabbar parent control should be who?
  • From the above analysis can probably also know that if we add tabbar to the navigation bar, we need to meet the requirements are satisfied.
  • To make it easier for everyone to use, I first define a placeholder container view jplinkcontainerview, I add it to the navigation bar, as a child control of the navigation bar, and later, just add your own Tabbar control to the placeholder view.
  • When we add the linkage Jplinkcontainerview to the navigation bar, the linkage view goes beyond the scope of the parent control and cannot respond to the Click event. So the first thing we need to do is customize a jpnavigationbar to replace the system's navigation bar. With the help of KVC, we can achieve this.
    setValue:navBar forKey:@"navigationBar"];
  • Now that we have customized the navigation bar, we can overload the HitTest method in Jpnavigationbar, traverse its own child control in this method, and find the Jplinkcontainerview we added. When the clicked Point is on the jplinkcontainerview of the placeholder container view We added, we manually distribute the Click event to our custom Tabbar child control to complete the event delivery of the responder chain.

    -(UIView *) HitTest: (Cgpoint) point withevent: (Uievent *) event{Jplinkcontainerview *linkview;for (uiview *subview in self.subviews) {if ([Subview Iskindofclass:[jplinkcontainerview class]] {Linkview = (Jplinkcontainerview *) Subview; break;} } cgpoint viewp = [self convertpoint:point ToView:linkView]; Span class= "Hljs-keyword" >if ([Linkview pointinside:viewp withevent:event]) {// If the clicked point is on the linkage view Linkview, the Linkview responds to the event return [Linkview hittest:viewp withevent:event]; Span class= "Hljs-keyword" >else{return [super hitTest:point Withevent:event]; //otherwise, execute the system default procedure}}           
04, use the attention point? 1. About the height of the linkage bottom view
@property(nonatomic)CGFloat jp_linkViewHeight;

Note: If your next interface requires a linkage bottom view, you can pass the value to me immediately after the last controller-(void) pushviewcontroller:animated: Method:

[self.navigationController pushViewController:YourOwnVC animated:YES];YourOwnVC.navigationController.jp_linkViewHeight = YourHopeHeight;

Also note: These two lines of code have a logical relationship, you must first call the push method, Navigationcontroller will alloc, allocate memory address, only the value.

2. About Linkage Bottom view
@property(nonatomic)JPLinkContainerView *jp_linkContainerView;

When you need to add a custom view, just add the custom view to the view.
Note: If you recognize that your current controller is Uitableviewcontroller, if you have a linkage bottom view, you will automatically add a jp_linkviewheight height to the bottom of the extra scrolling area. However, if your controller is adding uitableview on Uiviewcontroller, I will not automatically add the bottom additional scrolling area for you, and you will need to add contentinset for UITableView yourself.

05. What are the knowledge points used in this framework?
    • This framework uses a lot of runtime knowledge, including dynamic add properties, dynamic Add methods, Exchange methods, etc., if you are interested in runtime knowledge, welcome to read 1 lines of Code Fast integration button delay processing (hook combat).
    • The demo uses a cascading effect like Airbnb's head view, and if you're interested, you can check out my previous article on the TableView Head view cascade effect of Airbnb.
    • If you are interested in my previous implementation of a custom navigation bar, click here to customize the navigation bar (setting the navigation bar transparency and color for each interface).
06. Demo Address?

The frame's GitHub address is here [Jpnavigationcontroller]. If my article happens to help you in your actual work, please give a little star, thank you. Even more, if you and I love the Open source, love to share, perhaps a little hand shake, help me forward to more friends to see.



Wen/newpan (author of Jane's book)
Original link: http://www.jianshu.com/p/3ed21414551a
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

1 lines of code custom "TabBar"-B for each controller

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.