IOS开发中的UIScreen、UIView、UIWindow和UIViewController

1、UIScreen可以获取设备屏幕的大小。

1
2
3
4
5
6
7
// 整个屏幕的大小 {{0, 0}, {320, 480}}
CGRect bounds = [UIScreen mainScreen].bounds;
NSLog(@  "UIScreen bounds: %@"  , NSStringFromCGRect(bounds));
 
// 应用程序窗口大小 {{0, 20}, {320, 460}}
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
NSLog(@  "UIScreen applicationFrame: %@"  , NSStringFromCGRect(applicationFrame));

2、UIView对象定义了一个屏幕上的一个矩形区域,同时处理该区域的绘制和触屏事件。
可以在这个区域内绘制图形和文字,还可以接收用户的操作。一个UIView的实例可以包含和管理若干个子UIView。

ViewController.m

1
2
3
4
5
6
7
- (  void  )viewDidAppear:(  BOOL  )animated
{
      [super viewDidAppear:animated];
      UIView* myView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
      myView.backgroundColor=[UIColor redColor];
      [self.view addSubview:myView];
}

3、UIWindow对象是所有UIView的根,管理和协调的应用程序的显示
UIWindow类是UIView的子类,可以看作是特殊的UIView。一般应用程序只有一个UIWindow对象,即使有多个UIWindow对象,也只有一个UIWindow可以接受到用户的触屏事件。

AppDelegate.m

1
2
3
4
5
6
7
8
- (  BOOL  )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      UIWindow *myWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      myWindow.backgroundColor=[UIColor whiteColor];
      [myWindow makeKeyAndVisible];
      _window = myWindow;
      return  YES;
}

 4、UIViewController对象负责管理所有UIView的层次结构,并响应设备的方向变化。

AppDelegate.m

1
2
3
4
5
6
7
8
9
10
- (  BOOL  )application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
      UIWindow *myWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      myWindow.backgroundColor=[UIColor whiteColor];
      UIViewController *myViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
      myWindow.rootViewController = myViewController;
      [myWindow makeKeyAndVisible];
      _window = myWindow;
      return  YES;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值