iOS UI 之 简单概述

本文详细介绍了iOS APP UI的组成结构,包括UIWindow、UIView及其层级结构和UIViewController的作用及实现方式。解释了UIView的绘制周期和UIView的布局调整方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 iOS APP UI组成

   简单来说iOS APP UI一般由一个UIWindow 和 一系列层次化的UIView (UIView hierarchy) 及其背后一系列层次化的UIViewController(UIViewController hierarchy)组成。下面再接着分别讲一下组成iOS APP UI的三个重要元素:UIWindow,UIView和UIViewController。

1 UIWindow

   一般一个APP只有一个UIWindow,一般都是在APP启动的时侯创建。这个UIWindow通常有且只有一个根view(root view),也就是UIWindow充当这个root view的容器。
     在iOS4后一般通过以下代码为UIWindow添加root view
[self.window setRootViewController:self.viewController];
    

2 UIView

   APP的所有界面一般都是通过UIView来呈现的。UIView 管理着屏幕上一片长方形的区域,上面提到UIView hierarchy,一个UIView可以添加到另外一个UIView,也可以被添加多个UIView,由此组成UIView hierarchy。添加的UIView为subView, 被添加的是superView,互相之间形成一个父子关系。UIView,UIWindow(其实它也是UIView的子类)的坐标系零点在屏幕左上角。frame,bounds,center等几个属性定义了UIView的尺寸和坐标。 subView的坐标位置是相对于它的superView来的。坐标可以在UIView之间,UIView于UIWindow之间相互转换。
   convertPoint:fromView:
   convertRect:fromView:
   convertPoint:toView:
   convertRect:toView:
<pre name="code" class="objc">   convertPoint:fromWindow:
   convertRect:fromWindow:
   convertPoint:toWindow:
   convertRect:toWindow:


  UIView的描绘周期

The UIView class uses an on-demand drawing model for presenting content. When a view first appears on the screen, the system asks it to draw its content. The system captures a snapshot of this content and uses that snapshot as the view’s visual representation. If you never change the view’s content, the view’s drawing code may never be called again. The snapshot image is reused for most operations involving the view. If you do change the content, you notify the system that the view has changed. The view then repeats the process of drawing the view and capturing a snapshot of the new results.
When the contents of your view change, you do not redraw those changes directly. Instead, you invalidate the view using either the setNeedsDisplay or setNeedsDisplayInRect: method. These methods tell the system that the contents of the view changed and need to be redrawn at the next opportunity. The system waits until the end of the current run loop before initiating any drawing operations. This delay gives you a chance to invalidate multiple views, add or remove views from your hierarchy, hide views, resize views, and reposition views all at once. All of the changes you make are then reflected at the same time.
  When the time comes to render your view’s content, the actual drawing process varies depending on the view and its configuration. System views typically implement private drawing methods to render their content. Those same system views often expose interfaces that you can use to configure the view’s actual appearance. For custom UIView subclasses, you typically override the drawRect: method of your view and use that method to draw your view’s content. There are also other ways to provide a view’s content, such as setting the contents of the underlying layer directly, but overriding the drawRect: method is the most common technique.

   当我们需要UIView重绘的时侯可以发送一个setNeedsDisplay消息给UIView,为此UIView最好重写drawRect:方法。
   当我们需要手动通知UIView重新布局的时侯可以发送setNeedsLayout消息给UIView,为此UIView最好重写layoutSubviews方法。

  此外UIView还有其他重要属性如:contentMode,autoresizingMask等,不一而足。

3 UIViewController
   
   UI可以说由UIView hierarchy组成,也可以说是由UIViewController hierarchy组成,因为UIViewController一般都关联着一个UIView。把UIViewController关联的UIView呈现在屏幕上面有三种途径:
   1 作为UIWIndow的root ViewController
   2 作为子view添加到另外一个UIViewController的view hierarchy里面去。被添加的UIViewController为parent view controller(也是一个容器controller),添加的是child view controller。
   3 由一个UIViewController present出来。

Parent view controller和 child view controller可以嵌套使用,从而构成UIViewController hierarchy。iOS为我们提供的最为常用的parent view controller有UINavigationController和UITabbarController,我们也可以定制需要的parent view controller。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值