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.
1023 0
|
19天前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
6天前
|
开发框架 Android开发 iOS开发
安卓与iOS开发中的跨平台策略:一次编码,多平台部署
在移动应用开发的广阔天地中,安卓和iOS两大阵营各占一方。随着技术的发展,跨平台开发框架应运而生,它们承诺着“一次编码,到处运行”的便捷。本文将深入探讨跨平台开发的现状、挑战以及未来趋势,同时通过代码示例揭示跨平台工具的实际运用。
|
10天前
|
Java 调度 Android开发
安卓与iOS开发中的线程管理差异解析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自拥有独特的魅力。如同东西方文化的差异,它们在处理多线程任务时也展现出不同的哲学。本文将带你穿梭于这两个平台之间,比较它们在线程管理上的核心理念、实现方式及性能考量,助你成为跨平台的编程高手。
|
12天前
|
存储 前端开发 Swift
探索iOS开发:从新手到专家的旅程
本文将带您领略iOS开发的奇妙之旅,从基础概念的理解到高级技巧的掌握,逐步深入iOS的世界。文章不仅分享技术知识,还鼓励读者在编程之路上保持好奇心和创新精神,实现个人成长与技术突破。
|
15天前
|
安全 IDE Swift
探索iOS开发之旅:从初学者到专家
在这篇文章中,我们将一起踏上iOS开发的旅程,从基础概念的理解到深入掌握核心技术。无论你是编程新手还是希望提升技能的开发者,这里都有你需要的指南和启示。我们将通过实际案例和代码示例,展示如何构建一个功能齐全的iOS应用。准备好了吗?让我们一起开始吧!
|
20天前
|
安全 Swift iOS开发
Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法
本文深入探讨了 Swift 与 UIKit 在 iOS 应用界面开发中的关键技术和实践方法。Swift 以其简洁、高效和类型安全的特点,结合 UIKit 丰富的组件和功能,为开发者提供了强大的工具。文章从 Swift 的语法优势、类型安全、编程模型以及与 UIKit 的集成,到 UIKit 的主要组件和功能,再到构建界面的实践技巧和实际案例分析,全面介绍了如何利用这些技术创建高质量的用户界面。
23 2
|
27天前
|
安全 数据处理 Swift
深入探索iOS开发中的Swift语言特性
本文旨在为开发者提供对Swift语言在iOS平台开发的深度理解,涵盖从基础语法到高级特性的全面分析。通过具体案例和代码示例,揭示Swift如何简化编程过程、提高代码效率,并促进iOS应用的创新。文章不仅适合初学者作为入门指南,也适合有经验的开发者深化对Swift语言的认识。
45 9
|
22天前
|
vr&ar Android开发 iOS开发
安卓与iOS开发中的用户界面设计原则
【10月更文挑战第41天】探索移动应用开发的精髓,本文将深入分析安卓和iOS平台上用户界面设计的核心原则。通过比较两大操作系统的设计哲学,我们将揭示如何打造直观、易用且美观的应用程序界面。无论你是初学者还是资深开发者,这篇文章都将为你提供宝贵的见解和实用的技巧,帮助你在竞争激烈的应用市场中脱颖而出。
|
23天前
|
设计模式 Swift iOS开发
探索iOS开发:从基础到高级,打造你的第一款App
【10月更文挑战第40天】在这个数字时代,掌握移动应用开发已成为许多技术爱好者的梦想。本文将带你走进iOS开发的世界,从最基础的概念出发,逐步深入到高级功能实现,最终指导你完成自己的第一款App。无论你是编程新手还是有志于扩展技能的开发者,这篇文章都将为你提供一条清晰的学习路径。让我们一起开始这段旅程吧!