Note of IOS 7 - Views

简介: 1. Views   presentation: A view (an object whose class is UIView or a subclass of UIView) knows how to draw itself into a rectangular area of the interface.

1. Views

 

presentation:

A view (an object whose class is UIView or a subclass of UIView) knows how to draw itself into a rectangular area of the interface.

eg: you can drag an interface widget, such as a UIButton, into a view in the nib editor; when the app runs, the button appears, and works properly.

 

interact:

A view is also a responder (UIView is a subclass of UIResponder).
This means that a view is subject to user interactions, such as taps(轻敲) and swipes(猛击).
Thus, views are the basis not only of the interface that the user sees,
but also of the interface that the user touches.

 

A view may come from a nib, or you can create it in code. On balance, neither approach
is to be preferred over the other; it depends on your needs and inclinations(倾向) and on the
overall architecture of your app.

 

2. The Window

 

The top of the view hierarchy is the app’s window.

It is an instance of UIWindow, which is a UIView subclass. Your app should have exactly one main window.

It is created at launch time and is never destroyed or replaced.

It occupies the entire screen and forms the background to, and is the ultimate superview of, all your other visible views.

The window must fill the device’s screen.

Therefore, its size and position must be identical to the size and position of the screen.

This is done by setting the window’s frame to the screen’s bounds as the window is instantiated.

eg:

UIWindow* w = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

 

The window must persist for the lifetime of the app. To make this happen, the app
delegate class has been given a window property with a strong retain policy.

App without a main storyboard

If your app has no main storyboard, then creation and configuration of the window
must be done in some other way. Typically, it is done in code.

in application:didFinishLaunchingWithOptions:, like this:

self.window =
[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

 

You can drag a view from the Object library into the main view as a subview,
and it will be instantiated in the interface when the app runs.

Alternatively, you can create views and add them to the interface in code;

the simplest place to do this, for now, is the view controller’s viewDidLoad method, which has a reference to the view controller’s
main view as self.view.

eg:

- (void)viewDidLoad {
  [super viewDidLoad];
  UIView* mainview = self.view;
  UIView* v = [[UIView alloc] initWithFrame:CGRectMake(100,100,50,50)];
  v.backgroundColor = [UIColor redColor]; // small red square
  [mainview addSubview: v]; // add it to main view
}

 

Alternatively, you can start your project with the Empty Application template. It has
no .xib or .storyboard file, so your views will have to be created entirely in code. The
Empty Application template does not supply any view controllers, and does not assign
any view controller to the window’s rootViewController property.

 

A simple solution is to put your code in the app delegate’s application:didFinishLaunchingWithOptions:,

creating a minimal root view controller and accessing its main view through its view property.

eg: 

- (BOOL)application:(UIApplication *)application
  didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  
  // (template code:)
  self.window =
    [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 
  // Override point for customization after application launch.
  // (your code:)
  self.window.rootViewController = [UIViewController new];
  UIView* mainview = self.window.rootViewController.view;
  UIView* v = [[UIView alloc] initWithFrame:CGRectMake(100,100,50,50)];
  v.backgroundColor = [UIColor redColor]; // small red square
  [mainview addSubview: v]; // add it to the main view
  
  // (template code:)
  self.window.backgroundColor = [UIColor whiteColor];
  [self.window makeKeyAndVisible];
  
  return YES;
}

 

The method addSubview: makes one view a subview of another;

removeFromSuperview takes a subview out of its superview’s view hierarchy.

 

Oddly, there is no command for removing all of a view’s subviews at once.

However, a view’s subviews array is an immutable copy of the internal list of subviews, so it is legal
to cycle through it and remove each subview one at a time:

for (UIView* v in view.subviews)
  [v removeFromSuperview];

 

 Here’s an alternative way to do that:

[view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

 

3. Frame

A view’s frame property, a CGRect, is the position of its rectangle within its superview,
in the superview’s coordinate system.

 

UIView* v1 = [[UIView alloc] initWithFrame:CGRectMake(113, 111, 132,   194)];
v1.backgroundColor = [UIColor colorWithRed:1 green:.4 blue:1 alpha:1];

UIView* v2 = [[UIView alloc] initWithFrame:CGRectMake(41, 56, 132, 194)];
v2.backgroundColor = [UIColor colorWithRed:.5 green:1 blue:0 alpha:1];

UIView* v3 = [[UIView alloc] initWithFrame:CGRectMake(43, 197, 160, 230)];
v3.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];

[mainview addSubview: v1];
[v1 addSubview: v2];
[mainview addSubview: v3];


运行如下:

 

 

 

 

 

 

 

 

目录
相关文章
|
iOS开发 Windows
iOS Programming - Views(视图 - 基本绘制,变换,平移,旋转,反转,倾斜)
1. Views A view (an object whose class is UIView or a subclass of UIView) knows how to draw itself into a rectangular area of the interface.
976 0
|
11天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
116 3
|
11天前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
129 2
|
11天前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
48 0
|
11天前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
115 2
|
11天前
|
API 开发工具 Android开发
iOS 和 Android 平台的开发有哪些主要区别?
iOS与Android开发区别:iOS用Objective-C/Swift,App Store唯一下载渠道;Android用Java/Kotlin,多商店发布(如Google Play、华为市场)。设计上,iOS简洁一致,Android灵活可定制。开发工具,iOS用Xcode,Android用Android Studio。硬件和系统多样性,iOS统一,Android复杂。权限管理、审核流程及API各有特点,开发者需依据目标平台特性进行选择。
48 3
|
11天前
|
前端开发 Android开发 iOS开发
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
【4月更文挑战第30天】Flutter 框架实现跨平台移动应用,通过一致的 UI 渲染(Skia 引擎)、热重载功能和响应式框架提高开发效率和用户体验。然而,Android 和 iOS 的系统差异、渲染机制及编译过程影响性能。性能对比显示,iOS 可能因硬件优化提供更流畅体验,而 Android 更具灵活性和广泛硬件支持。开发者可采用代码、资源优化和特定平台优化策略,利用性能分析工具提升应用性能。
【Flutter前端技术开发专栏】Flutter在Android与iOS上的性能对比
|
11天前
|
存储 Swift iOS开发
使用Swift开发一个简单的iOS应用的详细步骤。
使用Swift开发iOS应用的步骤包括:创建Xcode项目,设计界面(Storyboard或代码),定义数据模型,实现业务逻辑,连接界面和逻辑,处理数据存储(如Core Data),添加网络请求(必要时),调试与测试,根据测试结果优化改进,最后提交至App Store或其它平台发布。
45 0
|
11天前
|
安全 Swift iOS开发
【Swift 开发专栏】Swift 与 UIKit:构建 iOS 应用界面
【4月更文挑战第30天】本文探讨了Swift和UIKit在构建iOS应用界面的关键技术和实践方法。Swift的简洁语法、类型安全和高效编程模型,加上与UIKit的紧密集成,使开发者能便捷地创建用户界面。UIKit提供视图、控制器、布局、动画和事件处理等功能,支持灵活的界面设计。实践中,遵循设计原则,合理组织视图层次,运用布局和动画,以及实现响应式设计,能提升界面质量和用户体验。文章通过登录、列表和详情界面的实际案例展示了Swift与UIKit的结合应用。
|
11天前
|
存储 安全 Swift
【Swift 开发专栏】使用 Swift 开发一个简单的 iOS 应用
【4月更文挑战第30天】本文介绍了使用 Swift 开发简单 iOS 待办事项应用的步骤。首先,阐述了 iOS 开发的吸引力及 Swift 语言的优势。接着,详细说明了应用的需求和设计,包括添加、查看和删除待办事项的功能。开发步骤包括创建项目、界面搭建、数据存储、功能实现,并提供了相关代码示例。最后,强调了实际开发中需注意的细节和优化,旨在帮助初学者掌握 Swift 和 iOS 开发基础。