Ios-view, ios View
A view is what you see on the screen and what you interact with by touching the screen. Therefore, a view is a main component of the visualization and touchability of an application.
1.1 subview and parent View
UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 200, 200)];v1.backgroundColor = [UIColor redColor];UIView *v2 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];v2.backgroundColor = [UIColor greenColor];UIView *V3 = [[UIView alloc] initWithFrame:CGRectMake(70, 70, 80, 80)];V3.backgroundColor = [UIColor blackColor]; [self.view addSubview:v1];[v1 addSubview:v2];[v1 addSubview:V3];
The code above creates the view v1 v2 v3, and v2 v3 is the subview of v1.
Running result: at different levels: the parent view is drawn first.
When the level is the same: Draw the view added "earlier" first
Other effects of view hierarchies:
If a view is removed or moved from its parent view, its child view will be associated with it.
If the size of a view changes, its child view can automatically adjust the size.
The transparency of a view is inherited by the quilt view.
A view can selectively restrict the rendering of its subviews so that any part outside the view is not displayed. This is called cut and is controlled by setting the clipsToBounds attribute of the view.
Parent views have their child views. From the perspective of memory management, they are similar to NSArray's element, it retains them and releases them when the child view is no longer its child view (removed from the Child view set of the View) or does not exist.
UIView has a superview attribute (A UIView) and a subviews attribute (an NSArray consisting of a UIView ).
1.2 application window
The top layer of a view is the window UIWindow (a subclass of UIView). An application has at least one UIWindow instance, which occupies the entire screen and is the final parent view of all other visible views.
There are three ways to create an application window:
The first method is to create an image by code without using xib or a series graph. The Code is as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; [self.window makeKeyAndVisible]; return YES;}
Application: didfinishlaunchingwitexceptions: A method delegated by the application. This method is called when the application is started. The UIWindow instance must exist throughout the lifecycle of the application, therefore, a window is retained in the delegate, and the makeKeyAndVisible message is used to display the application interface.
The second is to use a series chart:
This method does not need to write any code. My current xcode version is 6.0. When you select single view for a project to be created, a series chart (. storyboard file), in Info. you can specify the "Main storyboard file base name" in the plist file. It is already specified by default. You can also create a storyboard file and re-specify it.
In this case, after the UIApplicationMain instantiates the application delegate, it will request the delegate's window attribute. If it is nil, UIApplicationMain will create a UIWindow instance and assign it to the Delegate's window attribute, the initial view controller (initial view controller) of the connected graph is instantiated and assigned to the rootViewController attribute of the window. Then, the makeKeyAndVisible message is sent to the window for display. These are all completed by the UIApplicationMain in the background, so application: didfinishlaunchingwitexceptions: The implementation of the method is empty.
The third is to use the xib file:
1. Delete the "Main storyboard file base name" item in the Info. plist file.
2. Create an xib file and drag a Window to the canvas (including a Window ).
3. Set the class in the Identity inspector of the File's Owner in the xib File to the application proxy class.
4. Add the following attributes to the proxy class:
@property (strong, nonatomic) IBOutlet UIWindow *window;
(The header file of the proxy class originally has the window attribute, just change it to the above one)
5. Connect the window property.
6. Load the xib file in application: didfinishlaunchingwitexceptions: method. The Code is as follows:
[[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil];
1.3 important attributes of a view
1 frame declaration:
@property(nonatomic) CGRect frame;
Frame indicates the position of the view matrix in the parent view. It uses the coordinate system of the parent view. By default, the coordinate system of the parent view takes the upper left corner as the origin and the x coordinate to the right, if the y coordinate is down, the frame should be set during the view creation. If the frame is not set above, the default value is {0}, {0 0 }}.
2 bounds declaration:
@property(nonatomic) CGRect bounds;
Bounds and frame have only one difference,
Frame indicates the position and size of the view in the parent view coordinate system. (The reference point is the father's coordinate system)
Bounds indicates the position and size of the view in its own coordinate system. (The reference point is its own coordinate system)
The 3 center statement is as follows:
@property(nonatomic) CGPoint center;
Both the literal meaning and the type of the center indicate a central point, and the reference point, like bounds, is its own coordinate system.
4. The transform statement is as follows:
@property(nonatomic) CGAffineTransform transform;
Transform is used to provide the transformation of the rotation, scaling, and translation sub-classes. By default, the transform value is CGAffineTransformIdentity (no visible effect). Any transformation of the view is performed around the center, and the center remains unchanged.
Change the transform value using a function starting with "CGAffineTransformMake.
Demo:
UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(50, 150, 200, 200)];v1.backgroundColor = [UIColor redColor];[self.view addSubview:v1];v1.transform = CGAffineTransformMakeRotation(45 * M_PI/180);
The effects of other transformations are similar, so we will not demonstrate them one by one.
View switching in iOS development
There are two methods
1. Set the person who is window. rootViewcontroller as needed. Listen for notifications, log on and exit in appdelegate. h. When the window. rootViewcontroller is loaded by default, it is set as the logon viewcontroller. When the login succeeds, send a notification and receive the notification in appdelegate. The window will be modified. rootViewController is uitabbarcontroller. the corresponding exit notification is sent when you exit. In this case, the exit notification is received in appdelegate, and the window. rootViewController is set as the logon viewcontroller.
2. By default, uitabbarController is set to window. rootViewController. When you log on, use presentviewcontroller to bring up the logon viewcontroller. In this way, the first view is the logon view. After successful logon, dismiss logs on to viewcontroller and sends a notification to reload tabbarcontroller again. Exit and re-Press the logon window.
My suggestion is to use the second method. This method can be used to handle apps that do not need to log on or view some content. When you need to log on, press it to log on to the logon view.
How to delete a subview in ios development
If you want to find a specific sub-view in a view and remove it, the method is as follows:
// Traverse all subviews in self. view in sequence
For (id tmpView in [self. viewsubviews])
{
// Find the object of the Child view to be deleted
If ([tmpView isKindOfClass: [UIImageViewclass])
{
UIImageView * imgView = (UIImageView *) tmpView;
If (imgView. tag = 1) // determines whether the condition of the subview to be deleted is met.
{
[ImgView removeFromSuperview]; // deletes a subview.
Break; // jump out of the for Loop, because the sub-view has been found, you do not need to traverse down
}
}
}
If you want to completely release this view, you can directly release or autorelease it.
I hope the answer will be helpful to you. If you still have doubts, you can ask.