IOS开发之UIView
2019獨角獸企業重金招聘Python工程師標準>>>
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
? ? // Override point for customization after application launch.
? ? //1.創建window,設置frame坐標是(0,0)大小是屏幕大小
? ? //[UIScreen mainScreen].bounds]中bounds的類型是CGRect;
? ? //x和y值是0,w和h分別是屏幕的寬度和屏幕的高度
? ? _window = [[UIWindow alloc] initWithFrame:
?? ? ? ? ? ? ? [UIScreen mainScreen].bounds];
? ? //2.設置背景顏色
? ? [_window setBackgroundColor:[UIColor blackColor]];
?? ?
? ? //在這兒來創建界面
? ? //==============UIView=============
? ? //UIView是所有視圖類直接或者間接的父類,UIView所有的屬性和方法其他視圖類都有
??
? ? //(1)創建一個UIView對象:
? ? UIView *view1 = [[UIView alloc] init];
?? ?
? ? //(2)frame屬性:
? ? //視圖想要想要顯示在界面上,必須設置frame屬性,告訴系統顯示的位置和大小;
?? //frame屬性中的坐標是相對坐標,將當前視圖添加到哪個視圖上,就是相對于哪個視圖的坐標
? ? //OC中所有的結構體都對應一個make方法來快速的創建結構體變量
? ? [view1 setFrame:CGRectMake(0,20,200 ,100)];
? ? //(3)設置背景顏色(通過類方法創建顏色)
? ? [view1 setBackgroundColor:[UIColor cyanColor]];
? ? //(4)將這個視圖對象view1添加到指定的視圖上
? ? [_window addSubview:view1];
?? ?
? ? [view1 setTag:11];
?? ?
?? ?
?? ?
?? ?
? ? //=============再創建一個View==================
? ? //(1)創建一個UIView對象,并且設置其frame
? ? UIView *view2 = [[UIView alloc]initWithFrame:
? ? CGRectMake(10, 30, 50, 50)];
?? ?
? ? //(2)設置背景顏色
? ? [view2 setBackgroundColor:[UIColor redColor]];
?? ?
? ? //(3)將視圖添加到另外一個視圖上
? ? //_window是view2的父視圖,view2就是_window的子視圖
? ? [_window addSubview:view2];
?? ?
? ? //(4)設置tag值:
? ? //作用:a.父視圖通過tag值獲取指定的視圖,如果不設置所有視圖的tag值都是0
? ? //b.
? ? [view2 setTag:10];
?? ?
?? ?
?? ?
?? ?
? ? //3.設置為主窗口并顯示
? ? [_window makeKeyAndVisible];
?? ?
?? ?
?? ?
? ? return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
? ? //1.通過tag值去獲取當前視圖上的子視圖;
? ? [[_window viewWithTag:10] setBackgroundColor:[UIColor greenColor]];
?? ?
? ? //2.獲取當前視圖上的所有子視圖
? ? NSArray *viewArray = [_window subviews];
? ? NSLog(@"%@",viewArray);
? ? //遍歷數組中所有的視圖
? ? for (UIView *view in viewArray) {
?? ? ? ?
? ? ? ? if (view.tag == 11) {
?? ? ? ? ? ?
? ? ? ? ? ? [view setBackgroundColor:[UIColor yellowColor]];
?? ? ? ?
? ? ? ? }else if(view.tag == 10){
? ? ? ? ? ?
? ? ? ? ? ? view.backgroundColor = [UIColor whiteColor];
?? ? ? ? ? ?
? ? ? ? }
? ? }
?? ?
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
? ? // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
? ? // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
? ? // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
? ? // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {
? ? // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
轉載于:https://my.oschina.net/luhoney/blog/653093
總結
以上是生活随笔為你收集整理的IOS开发之UIView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 构建之法第四章读后感
- 下一篇: Elasticsearch——Searc