iOS開發——純程式碼介面(UITabBarController,UINavigationController,UIImageView)
阿新 • • 發佈:2019-01-24
一、建立UITabBarController和UINavigationController(標籤欄控制器和導航控制器)
建立兩個類,FirstViewController和SecondViewController。修改AppDelegate.m中的程式碼。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds ];
//建立tabBarController控制器
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//初始化firstView控制器
FirstViewController *firstView = [[FirstViewController alloc] init];
//初始化secondView控制器
SecondViewController *secondView = [[SecondViewController alloc]init];
//建立導航控制器nav1,顯示firstView
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:firstView];
//建立導航控制器nav2,顯示secondView
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:secondView];
//給TabBarController控制器新增子控制器nav1
[tabBarController addChildViewController:nav1];
//給TabBarController控制器新增子控制器nav2
[tabBarController addChildViewController:nav2];
//根控制器設定為TabBarController
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
FirstViewController.m中
- (void)viewDidLoad {
[super viewDidLoad];
//設定背景顏色為黃色
self.view.backgroundColor = [UIColor yellowColor];
}
SecondViewController.m中
- (void)viewDidLoad {
[super viewDidLoad];
//設定背景顏色為藍色
self.view.backgroundColor = [UIColor blueColor];
}
執行程式碼,效果圖如下
二、建立UIImageView
我在FirstViewController.m中建立一個UIImageView
- (void)viewDidLoad {
[super viewDidLoad];
//設定背景顏色為黃色
self.view.backgroundColor = [UIColor yellowColor];
UIImage *image = [UIImage imageNamed:@"Totoro副本"];
//初始化imageView把image設定為它的圖片
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//設定imageView的frame
imageView.frame = CGRectMake(50, 150, 280, 200);
//把imageView新增到View上
[self.view addSubview:imageView];
}
執行程式碼,效果如下