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(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];


运行如下:

 

 

 

 

 

 

 

 

posted @ 2014-02-09 10:57  Master HaKu  阅读(223)  评论(0编辑  收藏  举报