IOS开发之UIView
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
? ? // Override point for customization after application launch.
? ? //1.創(chuàng)建window,設(shè)置frame坐標(biāo)是(0,0)大小是屏幕大小
? ? //[UIScreen mainScreen].bounds]中bounds的類型是CGRect;
? ? //x和y值是0,w和h分別是屏幕的寬度和屏幕的高度
? ? _window = [[UIWindow alloc] initWithFrame:
?? ? ? ? ? ? ? [UIScreen mainScreen].bounds];
? ? //2.設(shè)置背景顏色
? ? [_window setBackgroundColor:[UIColor blackColor]];
?? ?
? ? //在這兒來(lái)創(chuàng)建界面
? ? //==============UIView=============
? ? //UIView是所有視圖類直接或者間接的父類,UIView所有的屬性和方法其他視圖類都有
??
? ? //(1)創(chuàng)建一個(gè)UIView對(duì)象:
? ? UIView *view1 = [[UIView alloc] init];
?? ?
? ? //(2)frame屬性:
? ? //視圖想要想要顯示在界面上,必須設(shè)置frame屬性,告訴系統(tǒng)顯示的位置和大小;
?? //frame屬性中的坐標(biāo)是相對(duì)坐標(biāo),將當(dāng)前視圖添加到哪個(gè)視圖上,就是相對(duì)于哪個(gè)視圖的坐標(biāo)
? ? //OC中所有的結(jié)構(gòu)體都對(duì)應(yīng)一個(gè)make方法來(lái)快速的創(chuàng)建結(jié)構(gòu)體變量
? ? [view1 setFrame:CGRectMake(0,20,200 ,100)];
? ? //(3)設(shè)置背景顏色(通過(guò)類方法創(chuàng)建顏色)
? ? [view1 setBackgroundColor:[UIColor cyanColor]];
? ? //(4)將這個(gè)視圖對(duì)象view1添加到指定的視圖上
? ? [_window addSubview:view1];
?? ?
? ? [view1 setTag:11];
?? ?
?? ?
?? ?
?? ?
? ? //=============再創(chuàng)建一個(gè)View==================
? ? //(1)創(chuàng)建一個(gè)UIView對(duì)象,并且設(shè)置其frame
? ? UIView *view2 = [[UIView alloc]initWithFrame:
? ? CGRectMake(10, 30, 50, 50)];
?? ?
? ? //(2)設(shè)置背景顏色
? ? [view2 setBackgroundColor:[UIColor redColor]];
?? ?
? ? //(3)將視圖添加到另外一個(gè)視圖上
? ? //_window是view2的父視圖,view2就是_window的子視圖
? ? [_window addSubview:view2];
?? ?
? ? //(4)設(shè)置tag值:
? ? //作用:a.父視圖通過(guò)tag值獲取指定的視圖,如果不設(shè)置所有視圖的tag值都是0
? ? //b.
? ? [view2 setTag:10];
?? ?
?? ?
?? ?
?? ?
? ? //3.設(shè)置為主窗口并顯示
? ? [_window makeKeyAndVisible];
?? ?
?? ?
?? ?
? ? return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
? ? //1.通過(guò)tag值去獲取當(dāng)前視圖上的子視圖;
? ? [[_window viewWithTag:10] setBackgroundColor:[UIColor greenColor]];
?? ?
? ? //2.獲取當(dāng)前視圖上的所有子視圖
? ? NSArray *viewArray = [_window subviews];
? ? NSLog(@"%@",viewArray);
? ? //遍歷數(shù)組中所有的視圖
? ? 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
轉(zhuǎn)載于:https://my.oschina.net/luhoney/blog/653093
總結(jié)
以上是生活随笔為你收集整理的IOS开发之UIView的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 构建之法第四章读后感
- 下一篇: Elasticsearch——Searc