create two file : AlterViewController.h and AlterViewController.m. .h file is the head file like C style. we can create class and declare property. .m file is is the implementation file it implement method and create instance object.
We should import UIKith to head file and then inherit from UIViewController
create implementation of AlterViewController. We should import AlterViewController.h and then implementation AlterViewController and override viewDidLoad method and so on .
UIView is flexible and easy to customize We can use iOS API to set its propety.We can set frame（position,size）、background color、gesture… etc. let’s look at the details.
UIView *alterView = [[UIView alloc]init];
in objective-c we can get an instance object use grammar like
Object *property = [[ Object alloc]init]
alloc return a new instance and initialized to a data structrue that describes the class memory for all other instance variables is set to 0;
In the first pleace we should have a concept of view coordinator system in UIKit .The default coordinate system has its origin in the top-left corner and has axes that extend down and to the right from the origin point .Firgure 1 show this concept
We can set UIView postion and size with frame rectangle. It create a rectangle return from CGRectMake Function that can define the position of rectangle
CGRectMake have four arguments: x,y rectangle width and height
CGRect CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);
you should get x,y-coordinate depends the origin point (0,0) at the top-left corner
For the example, I want to set UIView height with
160 and width with
Why subtract 30 ? because the X is 15 I want the UIView have a same safe area to the phone border The Firure 2 show the description .
The height same as the above. set height is 160 so I should subtract the View height and margin that’s why Y = screenHeight - 175 (175 = 160 +15)
For this section we set the UIView position and size completed.
We already create View and set the view position. we should add the view to the SuperView now
background: When we create view and want to display it add a view to the end of the receiver’s list of subviews is necessary. we can use
From the Apple API description:
the method establishes a strong reference to view and set its next responder to the receiver,which is its new superview. Views can have only one superview.If view already has a superview and that view is not receiver,this method removes the privious superview before making the receiver its new superview.
Add sub view method:
self.view rpresents the current view not the view we just created.
alterView will be presented soon.
If you want to listener the tap action we should add tap recognizer to the View.For example you want to dismiss the UIView when to tap the point out of UIView.
UITapGestureRecognizer object have a action of selector you should define the selector,eg: we can dismiss our UIView.
Now we can display the UIView on the other View.
- import AlterViewController.h file
- create instance of AlterViewController
- set presentation style
alter.modalPresentationStyle = UIModalPresentationOverCurrentContext;
- present view
[self presentViewController:alter animated:YES completion:nil];
Explain: when we present the view we the the modal style. you can found the guide from here
UIModalPresentationOverCurrentContext result like figure 3
Figure 3 The current context presentation style
@xizhou help me to understand the concepte of UITapGestureRecongnizer.