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 ...
随机推荐
- 《C++ Primer 4th》读书笔记 第3章-标准库类型
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3911534.html
- java 异常java.lang.UnsupportedOperationException
在项目中采用一个枚举的集合,本人采用Collections中的空集合Collections.emptyList()在添加时发生异常: 常见集合如下: private List<VacationC ...
- 转载RabbitMQ入门(4)--路由
路由 (使用Java客户端) 在先前的指南中,我们建立了一个简单的日志系统.我们可以将我们的日志信息广播到多个接收者. 在这部分的指南中,我们将要往其中添加一个功能-让仅仅订阅一个消息的子集成为可能. ...
- HDU 5387 Clock
题意:给一个时间,求三个时针之间的夹角,分数表示. 解法:算算算.统一了一下分母. 代码: #include<stdio.h> #include<iostream> #incl ...
- storm入门教程 第一章 前言[转]
1.1 实时流计算 互联网从诞生的第一时间起,对世界的最大的改变就是让信息能够实时交互,从而大大加速了各个环节的效率.正因为大家对信息实时响应.实时交互的需求,软件行业除了个人操作系统之外,数据库 ...
- bjfu1235 两圆公共面积
给定两个圆,求其覆盖的面积,其实也就是求其公共面积(然后用两圆面积和减去此值即得最后结果). 我一开始是用计算几何的方法做的,结果始终不过.代码如下: /* * Author : ben */ #in ...
- AutoLayout UITableViewCell 动态高度
从这里http://www.cnblogs.com/liandwufan/p/4516956.html?utm_source=tuicool 转载过来的 -(UITableViewCell*)tabl ...
- web安全测试&渗透测试之sql注入~~
渗透测试概念: 详见百度百科 http://baike.baidu.com/link?url=T3avJhH3_MunEIk9fPzEX5hcSv2IqQlhAfokBzAG4M1CztQrSbwsR ...
- MFC字体与文本输出
字体 成员函数 1.CFont( ); 构造一个CFont对象.此对象在使用之前应该先使用CreateFont.CreateFontIndirect.CreatePointFont或CreatePoi ...
- bzoj 2595 [Wc2008]游览计划(斯坦纳树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2595 [题意] 给定N*M的长方形,选最少权值和的格子使得要求的K个点连通. [科普] ...