IOS project-basic framework construction and ios basic framework construction

Source: Internet
Author: User
Tags define definition

IOS project-basic framework construction and ios basic framework construction

During project developmentIOS project -- build a project development environmentAfter that, we should first consider the overall framework and navigation architecture design of our project, and then consider the completion of functional modules on this basis.

1. Navigation Architecture Design

The navigation architecture design of an App should be in line with the inertia and convenience of operations, and should be more reasonable and humanized in interaction. Different App navigation designs should adopt different technical frameworks based on different project functions and positioning. Currently, the common navigation frameworks mainly include the tag, border, and matrix frameworks, for example, for the characteristics and details of each category, see:App navigation design.

Currently, the mainstream App frameworks of information and news mobile projects are basically tagged. This type of navigation design is basically built through TabBar + NavigationController framework, weibo and toutiao, which are frequently used, use TabBar + NavigationController. TabBar + NavigationController is to add itemBar to the TabBar. Each itemBar is an independent module, and each itemBar corresponds to a NavigationController. Because the navigators of each itemBar are different, you should first have TabBar and then NavigationController.

Our project also belongs to the Information class, so the framework of our project is also built using TabBar + NavigationController. Today, our main task is to build our TabBar.UITabBarController(Here is the official website document). Pay attention to the following points during use:

  • Each barItem must correspond to a ViewController, which can be a general ViewController or UINavigationController.
  • If the frame of ViewController or UINavigationController corresponding to barItem is set, be sure not to block the bottom TabBar
  • The order of barItem is the same as that of adding it to UITabBarController.
  • When the number of baritems added is <= 5, they are evenly distributed on the bottom bar, as shown in
  • When the number of baritems to be added is greater than 5, only the first four baritems to be added are displayed, and a more item is added. Click more to bring up an optional list, in addition, there is an edit button in the upper-right corner of the list. After clicking it, You can adjust the order of barItem display, as shown in.The number of baritems in the TabBar + NavigationController framework should not exceed 5. Otherwise, the user experience is not good.

  • You can use either setViewControllers of UITabBarController or addChildViewController of UIViewController to add a subview, you can also add data to a tabBar,However, when the number of baritems is greater than 5, only the first five items are displayed, and the remaining items are not displayed, or the more button is not available..

The sample code is as follows: Customize a TabBarController to inherit from UITabBarController, and then rewrite its viewDidLoad method to add sub-views and labels:

@ Implementation XMGTabBarController-(void) viewDidLoad {[super viewDidLoad]; // Add four item bar UITableViewController * vc0 = [[UITableViewController alloc] init]; vc0.view. backgroundColor = [UIColor redColor]; vc0.tabBarItem. title = @ "excellent"; vc0.tabBarItem. image = [UIImage imageNamed: @ "tabBar_essence_icon"]; vc0.tabBarItem. selectedImage = [UIImage imageNamed: @ "tabBar_essence_click_icon"]; [self addChildViewController: vc0]; UIViewController * vc1 = [[UIViewController alloc] init]; vc1.view. backgroundColor = [UIColor blueColor]; vc1.tabBarItem. title = @ "New Post"; vc1.tabBarItem. image = [UIImage imageNamed: @ "tabBar_new_icon"]; vc1.tabBarItem. selectedImage = [UIImage imageNamed: @ "tabBar_new_click_icon"]; [self addChildViewController: vc1]; UITableViewController * vc2 = [[UITableViewController alloc] init]; vc2.view. backgroundColor = [UIColor greenColor]; vc2.tabBarItem. title = @ "follow"; vc2.tabBarItem. image = [UIImage imageNamed: @ "tabBar_friendTrends_icon"]; vc2.tabBarItem. selectedImage = [UIImage imageNamed: @ "tabBar_friendTrends_click_icon"]; [self addChildViewController: vc2]; UIViewController * vc3 = [[UIViewController alloc] init]; vc3.view. backgroundColor = [UIColor grayColor]; vc3.tabBarItem. title = @ "I"; vc3.tabBarItem. image = [UIImage imageNamed: @ "tabBar_me_icon"]; vc3.tabBarItem. selectedImage = [UIImage imageNamed: @ "tabBar_me_click_icon"]; [self addChildViewController: vc3]; // The following method is also supported. We recommend that you use the following method. // [self addChildViewController: @ [vc0, vc1, vc2, vc3];}
2. Pay attention to code refactoring during development

In the development process, we 'd better not repeat the same code. Therefore, during the development process, we need to refactor and simplify our code, the main principle is to try to maintain a method to implement a function, and then try not to write duplicate code to streamline the logic. There are a lot of repetitive code in the code that adds tabBar item before us, so we need to refactor it and extract repetitive code, set different content as parameters for custom settings. The reconstructed logic is as follows:

@ Implementation XMGTabBarController-(void) viewDidLoad {[super viewDidLoad];/***** Add sub-controller *****/[self setupOneChildViewController: [[UITableViewController alloc] init] title: @ "essence" image: @ "tabBar_essence_icon" selectedImage: @ "tabBar_essence_click_icon"]; [self setupOneChildViewController: [[UITableViewController alloc] init] title: @ "" image: @ "tabBar_new_icon" selectedImage: @ "tabBar_new_click_icon"]; [self setupOneChildViewController: [[UIViewController alloc] init] title: @ "follow" image: @ "principal" selectedImage: @ "recommend"]; [self setupOneChildViewController: [[UITableViewController alloc] init] title: @ "I" image: @ "tabBar_me_icon" selectedImage: @ "tabBar_me_click_icon"];} /*** initialize a sub-controller ** @ param vc sub-controller * @ param title * @ param image icon * @ param selectedImage selected icon */-(void) setupOneChildViewController :( UIViewController *) vc title :( NSString *) title image :( NSString *) image selectedImage :( NSString *) selectedImage {vc. view. backgroundColor = [UIColor redColor]; vc. tabBarItem. title = title; vc. tabBarItem. image = [UIImage imageNamed: image]; vc. tabBarItem. selectedImage = [UIImage imageNamed: selectedImage]; [self addChildViewController: vc];} @ end
UITabBarItem settings

During iOS development, sometimes some images are automatically rendered in blue when the built-in space is displayed, for example, the selected TabBarItem image, you can also set a button for the UIButtonTypeSystem style. At this time, the system will automatically render the image in blue.

vc.tabBarItem.selectedImage = image;UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];[btn setImage:image forState:UIControlStateNormal];
3.1 image Rendering Problems

During the development process, we sometimes don't need this rendering. We only want the developed App to display the image as we set it. This is the setting and operation that we need to disable rendering of the image. There are two solutions:

  • Generate an image that will not be rendered again
    // Load the image UIImage * tempImage = [UIImage imageNamed: @ "tabBar_essence_click_icon"]; // generate an image UIImage * selectedImage = [tempImage imageWithRenderingMode: resize]; vc. tabBarItem. selectedImage = selectedImage;
  • Configure images directly in the xcassets file without rendering

 

3.2 set the text attribute of TabBarItem

After modifying the above pictures, although the tabBarItem pictures can be displayed completely according to the pictures we set, during the development process, we often need to set text attributes such as the font and font size of the title of tabBarItem. To set the text attribute of tabBarItem, we have two solutions:

  • Directly set each tabBarItem object
    // The literal attribute in normal state: normalAttrs = [NSMutableDictionary dictionary]; normalAttrs [dictionary] = [UIFont systemFontOfSize: 14]; normalAttrs [encoding] = [UIColor grayColor]; [vc. tabBarItem setTitleTextAttributes: normalAttrs forState: UIControlStateNormal]; // The text attribute * selectedAttrs = [NSMutableDictionary dictionary]; selectedAttrs [identifier] = [UIColor darkGrayColor]; [vc. tabBarItem setTitleTextAttributes: selectedAttrs forState: UIControlStateSelected];

    Note that:

    // Before key1.iOS7 used in the dictionary (in UIStringDrawing. can be found in h)-for example, UITextAttributeFont \ UITextAttributeTextColor-rule: UITextAttributeXXX2.iOS7 (in NSAttributedString. can be found in h)-for example, NSFontAttributeName \ NSForegroundColorAttributeName-rule: NSXXXAttributeName
  • Unified settings through the appearance object of UITabBarItem

    /***** Set the text attribute of all UITabBarItem *****/UITabBarItem * item = [UITabBarItem appearance]; // The literal attribute in normal state: normalAttrs = [NSMutableDictionary dictionary]; normalAttrs [dictionary] = [UIFont systemFontOfSize: 14]; normalAttrs [encoding] = [UIColor grayColor]; [item setTitleTextAttributes: normalAttrs forState: UIControlStateNormal]; // The text attribute optional * selectedAttrs = [Optional dictionary]; selectedAttrs [encoding] = [UIColor darkGrayColor]; [item setTitleTextAttributes: normalAttrs forState: UIControlStateSelected];
3.3 UIAppearance

As long as a class complies with UIAppearance, it can get the global appearance. UIview can get all the appearances. We can get all the tabBarItem exterior identifiers. However, this is generally not required, this method is used to obtain all the global tabBarItem exterior identifiers. during development, we generally develop the tabBarItem by ourselves. Therefore, we recommend that you use the following method, obtain only the tabBarItem identifier of the current class.

// Obtain the global tabBarItem appearance identifier UITabBarItem * item = [UITabBarItem appearance];
// Obtain all tabBarItemUITabBarItem * item = [UITabBarItem appearanceWhenContainedIn: self, nil] under the current class

Appearance Usage Note: It must be set before the control is displayed. It is usually placed in the + (void) load method instead of the + (void) initialize method, because + (void) the load method is called only once, and the + (void) initialize may be called multiple times. You also need to judge when using it.

  • Load Method: Called when the class is loaded. When will the class be loaded? The load method is called when the program moves together.
  • Initialize Method: Initialize the class. It is called when this class or subclass is used for the first time.
  • Viewdidload Method: Load the viewcontroller when it is about to be displayed for the first time.ViewController loads viewdidLoad only when it is displayed. However, only tabbarcontroller loads viewdidLoad when it is created.
Definition of four pch files

PCH File (Precompile Prefix Header File), that is, the pre-compiled Header File, its role is,This allows you to import header files, macros, or URL addresses that are used in multiple files at a time (global), which can effectively save time and improve development efficiency.. However, since Xcode 5, this file is no longer provided by default. If you want to continue using it, you need to manually create and configure it. The reason for the lack of provision by default may be due to the improvement of compilation efficiency. After all, pre-compilation will also increase the Build time.

For details about how to create and configure a PCH file, see:Create and configure a pch file in ios

The following are some notes for compiling PCH files:

# Ifndef PrefixHeader_pch # define PrefixHeader_pch/*** if you want to copy some content to any source code file (OC \ C ++, etc ), do not write the content between # ifdef _ OBJC _ and # endif ***** // ***** between # ifdef _ OBJC _ and # endif, it will only be copied to the OC source code file, it will not be copied to the source code files of other languages *****/# ifdef _ OBJC __# endif/***** in # ifdef _ OBJC _ and # endif content, it will only be copied to the OC source code file and will not be copied to the source code file of other languages *****/# endif
5. Configure Macros in Build Setting

In addition to the # define definition in the class, macro definition can also be configured in Build Setting during iOS development, however, the macro defined in Build Setting cannot be found in the project, that is, 【? ], You cannot jump to the corresponding defined position. At this time, it may be configured in Build Setting. For example, macro DEBUG we often see is configured in Build Setting.

Note: The macro names configured in Build Setting cannot all be lowercase letters.If all macro names are in lowercase, the following error occurs:

 

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.