国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

[iOS微博項目-1.0]-搭建基本框架

2019-11-14 19:35:39
字體:
來源:轉載
供稿:網友
A.搭建基本環境
 
 
項目結構:
Image(56)
 
1.使用代碼構建UI,不使用storyboard
Image(57)
 
 
AppDelegate:
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2     // Override point for customization after application launch. 3     4     // 啟動后顯示狀態欄 5     UIApplication *app = [UIApplication sharedApplication]; 6     app.statusBarHidden = NO; 7     8     // 設置window 9     self.window = [[UIWindow alloc] init];10     self.window.frame = [UIScreen mainScreen].bounds;11    12     [self.window makeKeyAndVisible];13    14     return YES;15 }
 
2.使用LaunchImage作為啟動圖,不使用xib
Image(58)
 
 
3.配置圖標AppIcon
Image(59)
 
不使用系統渲染圖標
Image(60)
 
4.設置屏幕方向-->只有豎向
5.啟動時隱藏狀態欄
Image(61)
 
 
B.項目分層 & 創建PCH
1.項目分層
為了讓在Finder中顯示跟Xcode中顯示都是分層效果,首先在Finder中建文件目錄層次
Image(62)
 
再把文件目錄拖入Xcode
Image(63)
 
2.創建并配置一個pch文件,來用聲明全局公共宏命令
Image(64)
 
配置:
Image(65)
 
 
C.添加子控制器
1.為每個Tab創建一個集成UITableViewController的類
分別是:首頁、信息、發現、我
2.創建一個集成UITabBarController的類作為window的rootViewController
AppDelegate:
 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2     // Override point for customization after application launch. 3     4     // 啟動后顯示狀態欄 5     UIApplication *app = [UIApplication sharedApplication]; 6     app.statusBarHidden = NO; 7     8     // 設置window 9     self.window = [[UIWindow alloc] init];10     self.window.frame = [UIScreen mainScreen].bounds;11    12     // 創建根控制器13     HVWTabBarViewController *tabVC = [[HVWTabBarViewController alloc] init];14     self.window.rootViewController = tabVC;15    16     [self.window makeKeyAndVisible];17    18     return YES;19 }
 
3.在上述的TabBarController中創建并添加子控件
HVWTabBarViewController.m :
 1 - (void)viewDidLoad { 2     [super viewDidLoad]; 3     // Do any additional setup after loading the view. 4     5     // 添加子控制器 6     // 首頁 7     HVWHomeViewController *homeVC = [[HVWHomeViewController alloc] init]; 8     homeVC.view.backgroundColor = [UIColor redColor]; 9     homeVC.title = @"首頁";10     [self addChildViewController:homeVC];11    12     // 消息13     HVWMessageViewController *messageVC = [[HVWMessageViewController alloc] init];14     messageVC.view.backgroundColor = [UIColor blueColor];15     messageVC.title = @"消息";16     [self addChildViewController:messageVC];17    18     // 發現19     HVWDiscoverViewController *discoverVC = [[HVWDiscoverViewController alloc] init];20     discoverVC.view.backgroundColor = [UIColor yellowColor];21     discoverVC.title = @"發現";22     [self addChildViewController:discoverVC];23    24     //25     HVWPRofileViewController *profileVC = [[HVWProfileViewController alloc] init];26     profileVC.view.backgroundColor = [UIColor greenColor];27     profileVC.title = @"";28     [self addChildViewController:profileVC];29 }
 
Image(66)
 
4.為tab添加圖片
1.需求:要區分iOS7之前及之后的系統,使用不同的圖片
這里創建一個UIImage的分類,新寫一個加載圖片的的方法,用來自動檢測系統版本并加載不同的圖片
 
(1)iOS6使用普通圖片, iOS7及以上系統版本使用的是帶有"_os7"結尾的圖片
Image(67)
 
(2)添加一條用來判別系統版本的宏
HVWWeibo-Prefix.pch:
 1 #ifndef HVWWeibo_HVWWeibo_Prefix_pch 2 #define HVWWeibo_HVWWeibo_Prefix_pch 3  4 // Include any system framework and library headers here that should be included in all compilation units. 5 // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file. 6  7 // 判別是否iOS7或以上版本系統 8 #define iOS7 ([UIDevice currentDevice].systemVersion.doubleValue >= 7.0) 9 10 #endif
 
(3)創建UIImage+Extension分類
UIImage+Extension.m:
 1 #import "UIImage+Extension.h" 2  3 @implementation UIImage (Extension) 4  5 + (UIImage *) imageWithNamed:(NSString *) imageName { 6     UIImage *image = nil; 7     8     // 如果是iOS7或以上版本 9     if (iOS7) {10         image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_os7", imageName]];11     }12    13     // 如果是iOS614     if (nil == image) {15         image = [UIImage imageNamed:imageName];16     }17    18     return image;19 }20 21 @end
 
(4)添加tab圖標
封裝一下創建子控制器的代碼
HVWTabBarViewController.m:
 1 #import "HVWTabBarViewController.h" 2 #import "HVWHomeViewController.h" 3 #import "HVWMessageViewController.h" 4 #import "HVWDiscoverViewController.h" 5 #import "HVWProfileViewController.h" 6 #import "UIImage+Extension.h" 7  8 @interface HVWTabBarViewController () 9 10 @end11 12 @implementation HVWTabBarViewController13 14 - (void)viewDidLoad {15     [super viewDidLoad];16     // Do any additional setup after loading the view.17    18     // 添加子控制器19     // 首頁20     HVWHomeViewController *homeVC = [[HVWHomeViewController alloc] init];21     [self addChildViewController:homeVC WithTitle:@"首頁" image:@"tabbar_home" seletectedImage:@"tabbar_home_selected"];22    23     // 消息24     HVWMessageViewController *messageVC = [[HVWMessageViewController alloc] init];25     [self addChildViewController:messageVC WithTitle:@"消息" image:@"tabbar_message_center" seletectedImage:@"tabbar_message_center_selected"];26    27     // 發現28     HVWDiscoverViewController *discoverVC = [[HVWDiscoverViewController alloc] init];29     [self addChildViewController:discoverVC WithTitle:@"發現" image:@"tabbar_discover" seletectedImage:@"tabbar_discover_selected"];30    31     //32     HVWProfileViewController *profileVC = [[HVWProfileViewController alloc] init];33     [self addChildViewController:profileVC WithTitle:@"" image:@"tabbar_profile" seletectedImage:@"tabbar_profile_selected"];34    35 }36 37 /** 添加tab子控制器 */38 - (void) addChildViewController:(UIViewController *) viewController WithTitle:(NSString *) title image:(NSString *) imageName seletectedImage:(NSString *) selectedImageName {39    40     // 設置隨機背景色41     viewController.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0  alpha:1.0];42    43     // 設置標題44     viewController.title = title;45     // 設置圖標46     viewController.tabBarItem.image = [UIImage imageWithNamed:imageName];47    48     // 被選中時圖標49     UIImage *selectedImage = [UIImage imageWithNamed:selectedImageName];50     // 如果是iOS7,不要渲染被選中的tab圖標(iOS7中會自動渲染成為藍色)51     if (iOS7) {52         selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];53     }54     viewController.tabBarItem.selectedImage = selectedImage;55    56     // 添加子控制器57     [self addChildViewController:viewController];58 }59  60 @end
 
 
Image(68)
 
 
D.添加導航控制器
1.只是在每個tab的controller上包裝了一個UINavigationController
HVWTabBarViewController.m:
 1 /** 添加tab子控制器 */ 2 - (void) addChildViewController:(UIViewController *) viewController WithTitle:(NSString *) title image:(NSString *) imageName seletectedImage:(NSString *) selectedImageName { 3     4     // 設置隨機背景色 5     viewController.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0  alpha:1.0]; 6     7     // 設置標題,直接設置title可以同時設置tabBarItem和navigationItem的title 8 //    viewController.tabBarItem.title = title; 9 //    viewController.navigationItem.title = title;10     viewController.title = title;11    12     // 設置圖標13     viewController.tabBarItem.image = [UIImage imageWithNamed:imageName];14    15     // 被選中時圖標16     UIImage *selectedImage = [UIImage imageWithNamed:selectedImageName];17     // 如果是iOS7,不要渲染被選中的tab圖標(iOS7中會自動渲染成為藍色)18     if (iOS7) {19         selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];20     }21     viewController.tabBarItem.selectedImage = selectedImage;22    23     // 添加子控制器24     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];25     [self addChildViewController:nav];26 }
 
Image(69)
 
 
2.進入非NavigationController的rootViewController的時候,隱藏底部的TabBar
自定義一個集成UINavigationController的類,代替原來的原生類
重寫pushViewController方法,當push的時候隱藏TabBar
#mark:此方法可以作用與所有的非rootViewController,非常好用
 
hideTabBarWhenPush
 
HVWNavigationViewController.m:
 1 #import "HVWNavigationViewController.h" 2  3 @interface HVWNavigationViewController () 4  5 @end 6  7 @implementation HVWNavigationViewController 8  9 - (void)viewDidLoad {10     [super viewDidLoad];11     // Do any additional setup after loading the view.12 }13 14 - (void)didReceiveMemoryWarning {15     [super didReceiveMemoryWarning];16     // Dispose of any resources that can be recreated.17 }18 19 /** 重寫push方法 */20 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {21     // 如果不是根控制器,隱藏TabBar22     if (self.viewControllers.count > 0) {23         // 注意這里不是self(navigationController),是push出來的ViewContoller隱藏TabBar24         viewController.hidesBottomBarWhenPushed = YES;25     }26    27     // 最后一定要調用父類的方法28     [super pushViewController:viewController animated:animated];29 }30 31 @end
 
E.添加導航欄按鈕
需要給各個Tab還有其下的頁面添加導航欄按鈕
 
 
1.在pch文件添加一個隨機顏色宏定義和一個debug模式下的log函數
 1 //  HVWWeibo-Prefix.pch 2 #ifndef HVWWeibo_HVWWeibo_Prefix_pch 3 #define HVWWeibo_HVWWeibo_Prefix_pch 4  5 // Include any system framework and library headers here that should be included in all compilation units. 6 // You will also need to set the Prefix Header build setting of one or more of your targets to reference this file. 7  8 #ifdef __OBJC__ 9     #import <UIKit/UIKit.h>10     #import <Foundation/Foundation.h>11     #import "UIImage+Extension.h"12 #endif13 14 // 測試用log15 #ifdef DEBUG16     #define HVWLog(...) NSLog(__VA_ARGS__)17 #else18     #define HVWLog(...)19 #endif20 21 // 判別是否iOS7或以上版本系統22 #define iOS7 ([UIDevice currentDevice].systemVersion.doubleValue >= 7.0)23 24 // 隨機顏色25 #define RandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0  blue:arc4random_uniform(256)/255.0  alpha:1.0]26 27 28 #endif
 
使用宏定義的log函數,只有在debug模式下才會轉化成為NSLog,release的時候會轉為空
1 /** 尋找朋友按鈕事件 */2 - (void) searchFriend {3     HVWLog(@"searchFriend");4 }
 
這里可以修改運行模式:
Image(70)
 
Image(71)
 
2.創建一個集成UIBarButtonItem的分類,用來創建使用UIButton作為按鈕圖標的item
 1 // 2 //  UIBarButtonItem+Extension.m 3 //  HVWWeibo 4 // 5 //  Created by hellovoidworld on 15/1/31. 6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8  9 #import "UIBarButtonItem+Extension.h"10 11 @implementation UIBarButtonItem (Extension)12 13 + (instancetype) itemWithImage:(NSString *) imageName hightlightedImage:(NSString *) highlightedImageName target:(id)target selector:(SEL)selector {14      UIBarButtonItem *item = [[self alloc] init];15    16     // 創建按鈕17     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];18     UIImage *image = [UIImage imageNamed:imageName];19     [button setImage:image forState:UIControlStateNormal];20     [button setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];21    22     // 一定要設置frame,才能顯示23     button.frame = CGRectMake(0, 0, image.size.width, image.size.height);24    25     // 設置事件26     [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];27    28     item.customView = button;29     return item;30 }31 32 @end
 
3.sample:在“首頁”頁面加上導航欄按鈕
Image(72)
 
 1 //  HVWHomeViewController.m 2 - (void)viewDidLoad { 3     [super viewDidLoad]; 4     5     // 添加導航控制器按鈕 6     // 左邊按鈕 7     self.navigationItem.leftBarButtonItem = [HVWBarButtonItem itemWithImage:@"navigationbar_friendsearch" hightlightedImage:@"navigationbar_friendsearch_highlighted" target:self selector:@selector(searchFriend)]; 8     9     // 右邊按鈕10     self.navigationItem.rightBarButtonItem = [HVWBarButtonItem itemWithImage:@"navigationbar_pop" hightlightedImage:@"navigationbar_pop_highlighted" target:self selector:@selector(pop)];11 }12  13 /** 左邊導航欄按鈕事件 */14 - (void) searchFriend {15     HVWLog(@"searchFriend");16 }17 18 /** 右邊導航欄按鈕事件 */19 - (void) pop {20     HVWLog(@"pop");21 }
 
4.給所有非rootViewController加上“返回”按鈕和“直接回到rootViewController”按鈕
在HVWNavigationViewController的push方法中實現
#mark:由于是在NavigationController中實現,可以一舉實現在所有非rootViewController中的效果。
 
NotRootNaviButton
 1 //  HVWNavigationViewController.m 2 /** 重寫push方法 */ 3 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { 4     // 如果不是根控制器,隱藏TabBar 5     if (self.viewControllers.count > 0) { 6         // 注意這里不是self(navigationController),是push出來的ViewContoller隱藏TabBar 7         viewController.hidesBottomBarWhenPushed = YES; 8         9         // 加上“返回上一層”按鈕和“直接回到根控制器”按鈕10         viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_back" hightlightedImage:@"navigationbar_back_highlighted" target:self selector:@selector(back)];11        12         viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_more" hightlightedImage:@"navigationbar_more_highlighted" target:self selector:@selector(more)];13     }14    15     // 最后一定要調用父類的方法16     [super pushViewController:viewController animated:animated];17 }18 19 /** 返回上一層 */20 - (void) back {21     [self popViewControllerAnimated:YES];22 }23 24 /** 返回根控制器 */25 - (void) more {26     [self popToRootViewControllerAnimated:YES];27 }

 

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐亭县| 镇康县| 东源县| 贵德县| 万载县| 敖汉旗| 万山特区| 毕节市| 桓台县| 天峨县| 乐安县| 海兴县| 广南县| 从化市| 黔西| 潼南县| 内黄县| 浙江省| 当雄县| 侯马市| 霍山县| 固原市| 奇台县| 雷波县| 聊城市| 格尔木市| 荆门市| 灵山县| 彩票| 临洮县| 竹北市| 中阳县| 宝坻区| 辽阳市| 灵宝市| 石台县| 中卫市| 垣曲县| 巫山县| 紫阳县| 塔城市|