源碼03-02-03-04-UIWindow

 

自定義窗口,不使用系統的storyboardwindows

 

 

//
//  main.m
//  03-UIWindow
#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

// main -> UIApplicationMain
/* UIApplicationMain 1.建立UIApplication 2.建立UIApplicationDelegate,而且成爲UIApplication代理 3.開啓主運行循環,保持程序一直在運行 4.加載info.plist,判斷有沒有指定main.stroyboard,指定了就加載 加載main.stroyboard作的事情 1.建立窗口 2.加載main.storyboard,而且加載main.storyboard指定的控制器 3.把新建立的控制器做爲窗口的跟控制器,讓窗口顯示出來  
 */

 

//
//  AppDelegate.m
//  03-UIWindow
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

// 程序啓動完成的時候
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 1.建立窗口,注意窗口必需要有尺寸,尺寸跟屏幕同樣大的尺寸,窗口不要被釋放
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];
    
    // 2.建立窗口的跟控制器
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];
    
    [vc.view addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];
    
    // 若是設置窗口的跟控制器,默認就會把控制器的view添加到窗口上
    // 設置窗口的跟控制器,默認就有旋轉功能
    self.window.rootViewController = vc; //    [self.window addSubview:vc.view];//這種設置自控件是沒有旋轉功能的;
    
    // 3.顯示窗口
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (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

 

 

04-UIWindow補充數組

//  AppDelegate.m
//  04-UIWindow補充
#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIWindow *window1;

@end

@implementation AppDelegate

//- (void)setWindow:(UIWindow *)window
//{
//    _window = window;
//    [UIApplication sharedApplication].windows
//}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window1 = [[UIWindow alloc] initWithFrame:CGRectMake(50, 50, 250, 250)];
    
    self.window1.backgroundColor = [UIColor yellowColor];
    
    // 設置窗口的層級關係
    self.window1.windowLevel = UIWindowLevelStatusBar;
    
    [self.window1 makeKeyAndVisible];

    
    // 窗口是有層級關係 // UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.windowLevel = UIWindowLevelAlert;
    
    self.window.backgroundColor = [UIColor redColor];
    
    [self.window makeKeyAndVisible];
    
    
    
    return YES;
}

- (void)keyboardIsWindow
{
    // 建立窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor yellowColor];
    
    // 判斷下鍵盤是否是窗口
//    NSLog(@"%@",application.windows);
    // 如何才能彈出鍵盤
    // 若是須要看到鍵盤,必需要把textField添加到一個View上面
    UITextField *textField = [[UITextField alloc] init];
    _textField = textField;
   [textField becomeFirstResponder];
    [self.window addSubview:textField];
    
//    NSLog(@"%@",application.windows);
    
    // 顯示窗口
    [self.window makeKeyAndVisible];

}

// 窗口:鍵盤,狀態欄也是窗口

#pragma mark - 窗口的顯示
- (void)windowWithVisible
{
    // 1.建立窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    //    self.window.backgroundColor = [UIColor greenColor];
    
    // application.windows:只要一給delegate的window屬性賦值,就會添加到windows數組。 //    NSLog(@"%@",application.windows);
    
    // 2.顯示窗口
    // makeKeyAndVisible:成爲app的主窗口而且顯示
    [self.window makeKeyAndVisible];
//    NSLog(@"%@",application.windows);
    //    self.window.hidden = NO;
    NSLog(@"%@",self.window);

}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (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
相關文章
相關標籤/搜索