iOS Development - Basic Framework

iOS Development - Basic Framework

[[164113]]

This basic framework mainly includes the extension of UITabBarController, UINavigationController and UIBarButtonItem classes. It mainly solves the problem of too many subviews being created, encapsulates the subview creation with UINavigationController, and then adds it to the ChildViewController view of UITabBarController. It also sets the font size and color of UITabBarItem of UITabBarController. Without further ado, here are the codes.

1. Inherit NPTabBarController created by UITabBarController

1. Set the font style on the tabbar

  1. #pragma mark - Set the text above the tabbar setTitleTextAttributes
  2. - ( void )setTabBarTitleAttributesStyle
  3. {
  4. NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
  5. //Usually the font size  
  6. attrs[NSFontAttributeName] = [UIFont systemFontOfSize: 16 ];
  7. //Usually the font color  
  8. attrs[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
  9.  
  10. NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
  11. //Select font size  
  12. selectAttrs[NSFontAttributeName] = [UIFont systemFontOfSize: 16 ];
  13. //Select font color  
  14. selectAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
  15.  
  16. //UI_APPEARANCE_SELECTOR Appearance  
  17. UITabBarItem *item = [UITabBarItem appearance];
  18. //tabbar usual font style  
  19. [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
  20. //tabbar selected font style  
  21. [item setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
  22. }

2. Navigation subview encapsulation

  1. #pragma mark - Navigation subview encapsulation
  2. - ( void )setChildVC:(UIViewController *)ChildVC title:(NSString *)title image:(NSString *)image selectImgage:(NSString *)selectImage {
  3.  
  4. //Note the view hierarchy. The top one is UINavigationController. Creating a view is to give a UINavigationController  
  5. NPNavigationController *nav = [[NPNavigationController alloc] initWithRootViewController:ChildVC];
  6. //The subview displays the tabbar title displayed on the UITabbarController  
  7. nav.tabBarItem.title = title;
  8. //The subview displays the unselected tabbar image on the UITabbarController  
  9. nav.tabBarItem.image = [UIImage imageNamed:image];
  10. //The subview displays the image of the selected tabbar on the UITabbarController  
  11. nav.tabBarItem.selectedImage = [UIImage imageNamed:selectImage];
  12. //Subview background color  
  13. nav.view.backgroundColor = [UIColor grayColor];
  14. //Note that the title displayed is the title of the subview, not UINavigationController  
  15. ChildVC.navigationItem.title = title;
  16. //Add subview  
  17. [self addChildViewController:nav];
  18.  
  19. }

2. Inherit UINavigationController to create NPNavigationController

1. Rewrite -(void)pushVewController:(UIViewConntroller*)viewCOntroller animated:(Bool)animated method

  1. - ( void )pushViewController:(UIViewController *)viewController animated:(BOOL)animated
  2.  
  3. //Judge whether self.childViewControllers has obtained all child controllers. When self.childViewControllers.count is 0, there are only child controllers, and the return button is disabled.  
  4. if (self.childViewControllers.count > 0 ) {
  5. //Customize the back button  
  6. UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  7. //Set title: return  
  8. [returnBtn setTitle:@ "return" forState:UIControlStateNormal];
  9. //Download image normally  
  10. [returnBtn setImage:[UIImage imageNamed:@ "black" ] forState:UIControlStateNormal];
  11. //Highlight image  
  12. [returnBtn setImage:[UIImage imageNamed:@ "grat" ] forState:UIControlStateHighlighted];
  13. //Normal title color  
  14. [returnBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  15. //Highlight title color  
  16. [returnBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
  17. //Set the button position and size. Note that the position has no effect here.  
  18. [returnBtn setFrame:CGRectMake( 0 , 0 , 70 , 20 )];
  19. //Button internal alignment  
  20. [returnBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
  21. //Set the internal position of the image  
  22. [returnBtn setContentEdgeInsets:UIEdgeInsetsMake( 0 , 0 , 0 , 0 )];
  23. //Add click event  
  24. [returnBtn addTarget:self action: @selector (returnBtnClick) forControlEvents:UIControlEventTouchUpInside];
  25. //Hide the tabbar when the view is pushed  
  26. viewController.hidesBottomBarWhenPushed = YES;
  27. //Customize the leftBarButtonItem of the push view  
  28. viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:returnBtn];
  29. }
  30. //The sub-controller pushes the view through this method  
  31. [ super pushViewController:viewController animated:animated];

3. Add the UIBarButtonItem class extension UIBarButtonItem + NPBarbutton, create the UIbarbuttonItem class method

1. UIbarbuttonItem class method

  1. /**
  2. *Create a UIBarbuttonItem class method. This method is a class method that creates a button. The parameters passed in are: image (normally displayed image), hightImage (highlighted image), target (target object), and action (click behavior).
  3. */  
  4. + (instancetype)itemWithImage:(NSString *)image hightImage:(NSString *)hightImage target:(id)target action:(SEL)action
  5. {
  6. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  7. [btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
  8. [btn setBackgroundImage:[UIImage imageNamed:hightImage] forState:UIControlStateNormal];
  9. //btnsize is the size of the Beijing picture  
  10. CGSize btnsize = btn.currentBackgroundImage.size;
  11. //Set the frame of btn  
  12. [btn setFrame:CGRectMake( 0 , 0 , btnsize.width, btnsize.height)];
  13. [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
  14. return [[self alloc] initWithCustomView:btn];
  15. }

The above is the main implementation of the basic framework. There are still some shortcomings, and the gesture sliding return is not written.

Promote MarkDown syntax link: http://www.jianshu.com/p/7cc9c26e8b7a Author: Damao Group

Download the demo of this article: http://code.cocoachina.com/view/129999 Author: NiePlus

<<:  Hello, Server Swift

>>:  [Bugly practical sharing] Android APP quick pad implementation

Recommend

The secret in the refrigerator: Who is secretly creating the crisis?

Modern families cannot live without refrigerators...

Is LeTV LeMe Bluetooth headset any good for a wireless experience at 99 yuan?

Electronic products surround the lives of modern ...

AppBase new applications (April 8) | "iTel Money Maker" takes the lead

All new apps on AppBase are in the entrants quadr...

Android N internal code name - New York Cheesecake

With the debut of the latest developer preview, t...

Drink juice or eat fruit? You may have been choosing the wrong one!

Fruits are nutritious and healthy, and are an ess...

Case Analysis: How to use the AARRR model to increase user growth?

This article takes Luckin Coffee as an example. I...

Build hybrid mobile apps with Apache Cordova

[[127393]] 【51CTO translation】 You have finished ...

Mobei SEO course tenth phase basic class quick start course plus advanced course

Getting Started: A First Look at SEO Purchase hos...

25 tips for Tik Tok operation and promotion!

As an Internet person who has been engaged in con...

Taobao store promotion and operation skills!

How to promote Taobao stores? This is a question ...

Are you suffering from developer ALS?

The rapid development of software development tec...