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.
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(,,,)];
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(,,,)];
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(, , , )];
v1.backgroundColor = [UIColor colorWithRed: green:. blue: alpha:]; UIView* v2 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v2.backgroundColor = [UIColor colorWithRed:. green: blue: alpha:]; UIView* v3 = [[UIView alloc] initWithFrame:CGRectMake(, , , )];
v3.backgroundColor = [UIColor colorWithRed: green: blue: alpha:]; [mainview addSubview: v1];
[v1 addSubview: v2];
[mainview addSubview: v3];
运行如下:

Note of IOS 7 - Views的更多相关文章
- Collection View Programming Guide for iOS---(一)----About iOS Collection Views
Next About iOS Collection Views 关于iOS Collection Views A collection view is a way to present an orde ...
- iOS Programming Views :Redrawing and UIScrollView
iOS Programming Views :Redrawing and UIScrollView 1.1 event You are going to see how views are red ...
- iOS Programming - Views(视图 - 基本绘制,变换,平移,旋转,反转,倾斜)
1. Views A view (an object whose class is UIView or a subclass of UIView) knows how to draw itself i ...
- iOS学习笔记(6)——翻译苹果文档About Windows and Views
About Windows and Views 关于窗口和视图 In iOS, you use windows and views to present your application’s cont ...
- iOS 9的新内容
https://www.hackingwithswift.com/ios9 Search extensibility Update: I wrote a tutorial on Core Spotli ...
- Project support for both iOS 6 and iOS 7
原文:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TransitionGuide/S ...
- Scoping the Project for iOS 7
Scoping the Project On This Page Things Every App Must Do Things Every App Should Do If You Must Con ...
- iOS 7 UI 过渡指南 - 支持续 iOS 6(iOS 7 UI Transition Guide - Supporting iOS 6)
iOS 7 UI Transition Guide Preparing for Transition Before You Start Scoping the Project Supporting i ...
- IOS 开发教程
http://www.raywenderlich.com/category/ios http://www.raywenderlich.com/50310/storyboards-tutorial-in ...
随机推荐
- 在Android应用中使用Clean架构
自从开始开发安卓应用,我一直感觉我可以做得更好.我看过不少烂代码,其中当然有我写的.安卓系统的复杂性加上烂代码势必酿成灾祸,所以从错误中成长就很重要.我Google了如何更好地开发应用,发现了这个叫做 ...
- SQLlite(WebSQL)如何排序并分页查询(SQLlite语法)
SELECT * FROM Table ORDER BY ID DESC Limit 10,9 limit语义:跳过10行,取9行 参考: SQLite的limit用法 如果我要去11-20的Ac ...
- 使用rsync同步Linux数据到Windows
windows: win7,cwrsyncserver 4.1.0 linux:ubuntu 14.04,rsync 3.1.0 networks:使用360wifi [Windows端] 是否使用管 ...
- hdu 3038 How Many Answers Are Wrong(种类并查集)2009 Multi-University Training Contest 13
了解了种类并查集,同时还知道了一个小技巧,这道题就比较容易了. 其实这是我碰到的第一道种类并查集,实在不会,只好看着别人的代码写.最后半懂不懂的写完了.然后又和别人的代码进行比较,还是不懂,但还是交了 ...
- IOS 点击按钮 光环 冲击波效果
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:ROUND_WIDTH/2 - ...
- 修改Android手机的“虚拟机堆大小”和android:largeHeap来防止APP内存溢出问题
使用“RAM Manager”修改“虚拟机堆大小”为某一个阀值 xxMB大小 修改 AndroidManifest.xml 里的 Application 标签的属性 android:largeHeap ...
- iOS学习网站及大牛网址(实时更新)
iOS学习网站及大牛网址(实时更新) 学习网站 https://github.com/Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库 https://github.co ...
- ASP.NET MVC之Html.RenderAction
WEB窗体模式开发惯了,切入MVC模式,好多东西都不懂,每一步都要查资料. 初步得来的一些知识点体会是: _Layout.cshtml就相当于母版页 然后partical视图(部分视图)就是用户控件. ...
- J2SE7规范_2013.2_类
8.1 类的定义 包括普通类和枚举类,枚举(略) 下面都是指普通类: public只能用于外部类,成员类,不能用于局部类,匿名类 protected和private用于成员类时(待解) sta ...
- 解决A program file was not specified in the launch configuration.问题
问题描述: 在eclipse 中开发c++或c是比较麻烦的事情,刚刚配置好mingw32和cdt和环境变量后,新建一个hello world的c++项目还是会出现问题.主要是在编译的时候会提示 ...