iOS Development Summary (A0)-Custom UIView

Source: Internet
Author: User

Based on my shallow iOS development experience, you can add custom UIView content in the following ways

1) Draw

2) Build in Xib

3) Add Subviews

In the M file of custom UIView, the UIView is typically initially set up as follows:

1-(void) awakefromnib{2 [self setup];3 }4-(void) setup{5 //Set up View6 }7-(Instancetype) initWithFrame: (cgrect) frame{8self=[Super Initwithframe:frame];9     if(self) {Ten [self setup]; One     } A     returnSelf ; -}

The general things to do in Setup are:

-SetBackgroundColor:

-Setcontentmode:

-Setopaque: (try to set to Yes)

-Settranslatesautoresizingmaskintoconstraints: (If you use AutoLayout, set to No, otherwise constraint may conflict)

The basic practices of these three methods are described below

First, Draw

Rewrite the UIView

-(void) DrawRect: (CGRect) rect{}

method to draw the contents of the UIView in the method.

-Can be painted with uibezierpath;

-can be used cgcontext various drawing functions;

-You can draw using the draw method that comes with the various controls in the Uikit

(such as UIImage's Drawinrect:, Nsattributedstring's drawinrect:)

-When setting the custom view content, the location-related properties, call [self setneeddisplay], the system will be drawn in a timely manner

Question: DrawRect: Can I use the Addsubview method in my method?

I understand: it is best not to use it, because it is possible for each draw to add Subview,

Question: How do I add uibutton?

My understanding: Add a button in Setup with Addsubview (do not set frame, the geometry of view is not determined), use Nslayoutcontraint to constrain the position of the button, or set the button's frame in Drawinrect:

Question: How to accept the user's touch/gesture

My understanding: Add gesture in Setup, or override the following methods to handle user interaction

-(void) Touchesbegan: (Nsset *) touches withevent: (uievent *)event{}-(void) Touchescancelled: (Nsset *) touches withevent: (uievent *)event{}-(void) touchesended: ( Nsset *) touches withevent: (uievent *)event{}-(void) touchesmoved: (Nsset *) touches Withevent: (uievent *)event{}

Note: Drawinrect: is done in the main queue, if Drawinrect: very complex, or need to draw a lot of view, the app may be stuck.

For this scenario, consider concurrently build interface (refer to WWDC video, followed by an essay specifically summarizing how to implement it)

Add view content with draw, the code is complex, but you can do real customization

Second, load from nib

1) Create a new view nib file, drag and drop the contents of the view in IB (Note that the view class is the class of the custom view), and set the constraint (how to set up the constraint, and a separate essay summary).

2) Use the following statement to create the view directly

[[[NSBundle mainbundle]loadnibnamed:@ ""                                  owner:nil                                Options:nil] Lastobject ];

Benefits (if you are familiar with IB):

1. Quick and easy to add content, set Iboutlet and ibaction, add gesture, etc.

2. Easy to localization

3. Easy to set constraint

Third, add Subviews

This method is more straightforward, add Subviews in Setup (this is not appropriate to set geometry information such as frame),

But it seems to be running less efficient.

iOS Development Summary (A0)-Custom UIView

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.