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 ...
随机推荐
- 【转】cocos2d-x Lua
Call custom c++ from Lua cocos2d-x lua binds c++ class, class functions ,enum and some global functi ...
- Oracle 课程八之性能优化之10046事件
Oracle 的事件很多. 具体参考blog: Oracle 跟踪事件 set event 转摘:http://blog.csdn.net/tianlesoftware/archive/2009/12 ...
- Lighting System Design
题意:从小到大给出额定功率,给出该功率费用,和灯泡的数量和单价,现在灯泡能在比他额定功率大的功率运行,求让所有灯泡正常工作的最小费用 分析: 问题转化为求用哪几个功率运行灯泡最小费用,dp[i]前i个 ...
- HDU 5679 Substring 后缀数组判重
题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...
- poj1247 bjfu1239水题
其实就是读题啦,读懂题很简单,就是问一个数组,存不存在一个点,按这个点切成两半,这两半的数字的和是一样的.不多说了,上代码 /* * Author : ben */ #include <cstd ...
- SSH 连接慢的解决方案详解
SSH 连接慢的解决方案详解 http://www.codeceo.com/article/ssh-slow.html
- 如何用Entity Framework 6 连接Sqlite数据库[转]
获取Sqlite 1.可以用NuGet程序包来获取,它也会自动下载EF6 2.在Sqlite官网上下载对应的版本:http://system.data.sqlite.org/index.html/do ...
- CSS基础(背景、文本、列表、表格、轮廓)
CSS 背景属性 属性 描述 background 简写属性,作用是将背景属性设置在一个声明中. background-attachment 背景图像是否固定或者随着页面的其余部分滚动. backgr ...
- 第二百五十天 how can I 坚持
html排版,好烦心. 我以为我会哭,但是我没有.---<领悟> 确实是搞不懂自己,到底想要什么?弟弟最近也不知道咋的了,感觉有点不对劲呢. 最近雾霾好严重,希望我们的后代不会知道雾霾是什 ...
- CoffeeScript学习(1)——Quick Start
什么是CoffeeScript CoffeeScript 是一门编译到 JavaScript 的小巧语言. 在 Java 般笨拙的外表下, JavaScript 其实有着一颗华丽的心脏. Coffee ...