IOS practice 01, ios practice

Source: Internet
Author: User

IOS practice 01, ios practice

Before last year's holiday, I probably completed the Sina Weibo project, and now I forgot about it. I plan to write it again. Some of the previous notes are written in SleenXiu on Sina's blog. Here, Sina Weibo is written in a casual form to facilitate future review.

First look at the main completed before several points, second shot video connection: http://video.weibo.com/show? Fid = 1034: 32ed06b90b1bba7ba25cc546a06fa949

Write it again today.

To build a classic framework, the so-called classic framework is the UITabBarController, which manages several uinavigationcontrollers. Different uinavigationcontrollers manage the corresponding VC.

1. Complete the preliminary framework construction in appdelegate. We need a custom UITabBarController and become the root controller of the window.

1-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {2 // Override point for customization after application launch. 3 4 // set window 5 self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds]; 6 7 // initialize your own tabbarController 8 SVTabbarController * tabbar = [[SVTabbarController alloc] init]; 9 10 // Add a root controller for the window, 11 self. window. rootViewController = tabbar; 12 13 // display window 14 [self. window makeKeyAndVisible]; 15 16 return YES; 17}

2. group files by module. The MVC structure is shown below.

1 // Add sub-controller 2-(void) addController 3 {4 SVHomeController * homeVC = [[SVHomeController alloc] init]; 5 SVNavigationController * homeNav = [[SVNavigationController alloc] initWithRootViewController: homeVC]; 6 homeVC. title = @ "Homepage"; 7 homeVC. tabBarItem. image = [UIImage imageNamed: @ "tabbar_home_os7"]; 8 homeVC. tabBarItem. selectedImage = [UIImage imageNamed: @ "tabbar_home_selected_os7"]; 9 [self addChildViewController: homeNav]; 10 11 SVHomeController * messageVC = [[SVHomeController alloc] init]; 12 SVNavigationController * messageNav = [[SVNavigationController alloc] initWithRootViewController: messageVC]; 13 messageVC. title = @ "message"; 14 messageVC. tabBarItem. image = [UIImage imageNamed: @ "tabbar_message_center_os7"]; 15 messageVC. tabBarItem. selectedImage = [UIImage imageNamed: @ "tabbar_message_center_selected_os7"]; 16 [self addChildViewController: messageNav]; 17 18 SVHomeController * discoverVC = [[SVHomeController alloc] init]; 19 SVNavigationController * discoverNav = [[SVNavigationController alloc] initWithRootViewController: discoverVC]; 20 discoverVC. title = @ ""; 21 discoverVC. tabBarItem. image = [UIImage imageNamed: @ "tabbar_discover_os7"]; 22 discoverVC. tabBarItem. selectedImage = [UIImage imageNamed: @ "tabbar_discover_selected_os7"]; 23 [self addChildViewController: discoverNav]; 24 25 SVHomeController * meVC = [[SVHomeController alloc] init]; 26 SVNavigationController * meNav = [[SVNavigationController alloc] initWithRootViewController: meVC]; 27 meVC. title = @ "me"; 28 meVC. tabBarItem. image = [UIImage imageNamed: @ "tabbar_profile_os7"]; 29 meVC. tabBarItem. selectedImage = [UIImage imageNamed: @ "tabbar_profile_selected_os7"]; 30 [self addChildViewController: meNav]; 31}

The basic effect is already available.

4. The tabbar navigation is not intended. Delete the built-in tabbaritem and add the custom tabbar and [self addtabbar].

1 // remove the system tabbar. When the view is about to appear, delete all items 2-(void) viewWillAppear :( BOOL) animated 3 {4 [super viewWillAppear: animated]; 5 6 for (UIView * child in self. tabBar. subviews) {7 if ([child isKindOfClass: [UIColor class]) {8 [child removeFromSuperview]; 9} 10} 11} 12 // Add your own tabbar13-(void) addTabbar14 {15 // initialize your own tabbar16 SVTabbar * tabbar = [[SVTabbar alloc] initWithFrame: self. tabBar. bounds]; 17 // save your tabbar18 self. mytabbar = tabbar; 19 // Add your own tabbar20 [self. tabBar addSubview: tabbar]; 21 // Add proxy 22 tabbar for tabbar. delegate = self; 23 24}

5. encapsulate your own tabbar, add controls, and customize the button item to complete the jump

5-1. add a control to add btn numbers based on the number of controllers. btn values are passed through items. In the tabbar,-(void) addBtnWithItem :( UITabbarItem) item is provided; method to add your own item (btn)
1-(void) addBtnWithItem :( UITabBarItem *) item 2 {3 // initialize btn 4 SVTabbarButton * btn = [[SVTabbarButton alloc] init]; 5 // save btn 6 [self. tabbarBtns addObject: btn]; 7 // value: btn 8 btn. item = item; 9 // Add btn10 [self addSubview: btn]; 11 // bind the click event to the button 12 [btn addTarget: self action: @ selector (selectedBtn :) forControlEvents: UIControlEventTouchUpInside]; 13 // the first 14 if (self. tabbarBtns. count = 1) {15 [self selectedBtn: btn]; 16} 17} 18 // Click Event 19-(void) selectedBtn :( SVTabbarButton *) btn20 {21 // notification proxy 22 if ([self. delegate respondsToSelector: @ selector (tabbar: didSelectBtnFrom: to :)]) {23 [self. delegate tabbar: self didSelectBtnFrom :( int) self. currentBtn. tag to :( int) btn. tag]; 24} 25 // set the selected tabbar 26 self. currentBtn. selected = NO; 27 btn. selected = YES; 28 self. currentBtn = btn; 29} 30 // layout button 31-(void) layoutSubviews32 {33 self. subviews [0]. center = CGPointMake (self. frame. size. width * 0.5, self. frame. size. height * 0.5); 34 35 for (int index = 0; index <self. tabbarBtns. count; index ++) {36 SVTabbarButton * btn = self. tabbarBtns [index]; 37 38 CGFloat bW = self. frame. size. width/self. subviews. count; 39 CGFloat bH = self. frame. size. height; 40 CGFloat bX = index * bW; 41 CGFloat bY = 0; 42 if (index> 1) {43 bX + = bW; 44} 45 btn. frame = CGRectMake (bX, bY, bW, bH); 46 47 btn. tag = index; 48} 49}
5-2. Customize the button, re-layout the button location, and assign values to the button data. There are four controllers. Therefore, rebuild the method of adding controllers and assign values to corresponding attributes based on different controllers.
1 // re-layout the btn internal control to get the desired effect 2-(instancetype) initWithFrame :( CGRect) frame 3 {4 if (self = [super initWithFrame: frame]) {5 // The picture is centered on 6 self. imageView. contentMode = UIViewContentModeCenter; 7 // The text Center shows 8 self. titleLabel. textAlignment = NSTextAlignmentCenter; 9 // set the font size to 10 [self. titleLabel setFont: [UIFont systemFontOfSize: 13]; 11 12 // set the font color 13 [self setTitleColor: [UIColor blackColor] forState: UIControlStateNormal]; 14 [self setTitleColor: [UIColor orangeColor] forState: UIControlStateSelected]; 15} 16 return self; 17} 18 // rewrite the set Method of item and assign a value of 19 to (void) setItem :( UITabBarItem *) to btn *) item20 {21 _ item = item; 22 23 [self setTitle: item. title forState: UIControlStateNormal]; 24 [self setImage: item. image forState: UIControlStateNormal]; 25 [self setImage: item. selectedImage forState: UIControlStateSelected]; 26} 27 // The Position of the internal image 28-(CGRect) imageRectForContentRect :( CGRect) contentRect29 {30 CGFloat imageW = contentRect. size. width; 31 CGFloat imageH = contentRect. size. height * 0.6; 32 return CGRectMake (0, 0, imageW, imageH); 33 34} 35 // position of the internal text 36-(CGRect) titleRectForContentRect :( CGRect) contentRect37 {38 CGFloat titleY = contentRect. size. height * 0.6; 39 CGFloat titleW = contentRect. size. width; 40 CGFloat titleH = contentRect. size. height-titleY; 41 return CGRectMake (0, titleY, titleW, titleH); 42} 43 // remove the highlighted status of the button 44-(void) setHighlighted :( BOOL) highlighted {}
5-3. click the button to jump to the Controller. Click Cancel to highlight the button. The first button is selected by default. click the button to bind the click event to the custom tabbar write proxy. Click the listener button to jump to the Controller.
1 @ protocol SVTabbarDelegate <NSObject> 2 @ option_3 // when the button on the tabbar is clicked, 4-(void) tabbar :( SVTabbar *) tabbar didSelectBtnFrom :( int) is called) from to :( int) to; 5 @ end
1 # proxy method of pragma mark tabbar 2-(void) tabbar :( SVTabbar *) tabbar didSelectBtnFrom :( int) from :( int) to3 {4 self. selectedIndex = to; 5}
6. Add the plus button in the middle, and the position is fixed, so you can set it directly once.
 1 - (instancetype)initWithFrame:(CGRect)frame 2 { 3     if (self = [super initWithFrame:frame]) { 4         UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 5          6         [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_os7"] forState:UIControlStateNormal]; 7         [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted_os7"] forState:UIControlStateHighlighted]; 8         [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_os7"] forState:UIControlStateNormal]; 9         [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted_os7"] forState:UIControlStateHighlighted];10         plusBtn.frame = CGRectMake(0, 0, plusBtn.currentBackgroundImage.size.width, plusBtn.currentBackgroundImage.size.height);11         12         [self addSubview:plusBtn];13     }14     return self;15 }

This is done first today, and the basic big framework is ready. I will fix the details tomorrow.

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.