IOS development UI basic UIView and iosuiuiview

Source: Internet
Author: User

IOS development UI basic UIView and iosuiuiview

This section mainly introduces the basic concepts of UIView and some attributes. The attribute users will be described in detail later.

-Basic concepts of UIView 1. What is a control?

All the UI elements on the screen are called controls (also called view components in many books). For example, buttons (UIButton) text (UILabel) are controls.

What are the common attributes of controls?

Dimensions

Location

Background Color

...........

Apple extracts the common attributes of controls to the parent class UIView. All controls are ultimately inherited from the UIView. UIBUtton UIView is inherited from the UIView.

 

Parent control and child Control

Each control is a container that can accommodate other controls.

The internal widget is a subcontrol of the big control.

A large widget is the parent widget of an internal widget.

Each controller (UIViewController) has a default UIView. All the other controls managed in the property controller are directly or indirectly subcontrols of this view.

@property(nonatomic,retain)UIView* view; 

 

2. Common attributes of UIView@ Property (nonatomic, readonly) UIView * superview; returns the parent control object of the primary node.

@ Property (nonatomic, readonly, copy) NSArray * subviews; assign to obtain all the parent child control objects of the primary node

 

@ Property (nonatomic) NSInteger tag; ID (identifier) of the primary control. The parent control of the primary control can use the tag to find the corresponding primary child control.

 

@ Property (nonatomic) CGAffineTransform transform; deformation attribute of the transform control (attributes such as rotation angle, proportional scaling, and translation can be set)

 

-(Void) addSubview :( UIView *) view; add a navigation sub-control view

-(Void) removeFromSuperview; removed from parent Control


-(UIView *) viewWithTag :( NSInteger) tag; the consumer identifies the corresponding control based on the tag of the consumer (the operator is generally a worker subcontrol)

Before introducing the following three attributes, we must first understand the following UIKit coordinate system:

In UIKit, the coordinate system's origin (0, 0) is in the upper left corner. The X value is in the right direction. The Y value is in the downward direction.

@ Property (nonatomic) CGRect frame; the rectangular frame of the rectangle control in the upper-right corner of the upper-right corner) CGRect bounds; the position and ruler of the rectangle frame of the rectangle control (starting from the upper left corner of the rectangle as the coordinate origin, so the x and y axes of bounds are generally 0) @ property (nonatomic) CGPoint center; point position of the vertex control (with the upper left corner of the parent control as the coordinate origin) UIView attribute setting example 1. alpha: sets the view transparency to 1 by default.
1 self. view. alpha = 0 // completely transparent 2 3 self. view. alpha = 1; // opaque

2. When clipsToBounds is set to YEW by default, content that exceeds the size of the current view and subviews are not displayed.

Self. view. clipsToBounds = YES; // content beyond the view Size and subview are not displayed

3. The default value of hidden is NO. When it is set to YES, the view will be invisible.

1 self. view. hidden = YES; // hide the current view

4. The default value of userInteractionEnabled is YES. If it is set to NO View, it cannot interact with the user and cannot respond to the event.

Self. view. userInteractionEnabled = NO; // you cannot interact with the user and cannot respond to events.

5. tag is 0 by default to mark

Self. view. tag = 10; // mark the view as 10. By default, all controls are marked as 0.

6. exclusiveTouch is NO by default.

ExclusiveTouch indicates: if the UIView with exclusiveTouch set is the first responder of the entire touch event, other uiviews before your fingers exit the screen will not be able to accept all the touch events throughout the event cycle.

7. CGRect frame

> Indicates the position and size of the control. The coordinate origin is (0, 0) in the upper left corner of the parent control)

> Modify this attribute to adjust the widget's position and size.

8. CGPoint center

> Indicates that the midpoint of the control takes the upper left corner of the parent control as the coordinate origin.

> Modify this attribute to adjust the widget location.

9. CGRect bounds

> It indicates that the position and size of the control are based on the coordinate origin in the upper left corner of the control. Generally, the position is (0, 0)

> You can only adjust the widget size when modifying this attribute.

10. CGAffineTransform transform

> Represents the scaling ratio of the control's deformation state Rotation Angle

> Create a CGAffineTransform Function

CGAffineTransformMakeScale (CGFloat scaleX, CGFloat scaleY); // create an x. The scaling ratio in the y direction is scaleX and the scaleY deformation value CGAffineTransformMakeRotation (CGFloat angle ); // create a CGAffineTransformScale (CGAffineTransform t, CGFloat sx, CGFloat sy) deformation value with the angle of rotation. // scale again based on the deformation value t, returns a new deformation value CGAffineTransformRotate (CGAffineTransform t, CGFloat angle). // Based on the deformation value t, rotate the image again. The rotation angle is angle, and a new deformation value is returned.

11. superview

Returns the parent view of the current view.

12. window: return the window of the current view.

When obtaining superview and window of the Root View, note that viewdidload cannot be obtained in viewdidload, which is called only when the view is loaded, at this time, the view is not added to the window. Therefore, you must obtain the view in the viewDidAppear method before the view is added to the window.

Example:

-(Void) viewDidLoad {[super viewDidLoad]; NSLog (@ "viewDidLoad ------- % @", self. view. superview); // NSLog (@ "viewDidLoad -------- % @", self. view. window); // No value}-(void) viewDidAppear :( BOOL) animated {NSLog (@ "viewDidAppear ----- % @", self. view. superview); // NSLog (@ "viewDidAppear ----- % @", self. view. window); // value}

 

13. The default value of autoresizesSubviews is YES, indicating that the child control will change as the size of the parent control changes.

14. autoresizingMask is set to UIViewAutoresizingNone by default and will not be automatically scaled.

15. Set content mode in contentMode

UIViewContentModeScaleToFill is not filled in any proportion according to the original width proportion (length and width increase. In this way, the view will not be blank and all content can be displayed.
UIViewContentModeAspectToFill is filled according to the ratio of original length to width, not all contents are displayed completely. In this way, the content may overflow, but the entire view will not be left blank.
UIViewContentModeAspectToFit displays all content in accordance with the original length/width ratio (length/width ratio. In this way, the left and right sides are easily left blank or left blank.

16. backgroundColor background color
Self. view. backgroundColor = [UIColor redColor]; // set the background color to red.

How to add a subview to a UIView
  • AddSubview adds a control to a control.
  • BringSubviewToFront move a control to the front
  • SendSubviewToBack
  • RemoveFromSuperview remove the control
  • InsertSubview: insert View Into atIndex and specify Index
  • InsertSubview: aboveSubview; insert a view to a view
  • InsertSubview: belowSubview; insert a view to a view
  • ExchangeSubviewAtIndex: withSubviewAtIndex:
Readers can try the usage of the above attributes one by one, without having to remember to understand them for the moment. I will explain the usage of each attribute in depth using some small cases.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.