Swift Pure Code custom control

Source: Internet
Author: User

1. Create a custom control
Import UIKit class Customview:uiview {var lab:uilabel!     var btn:uibutton! /************ will need to add the child controls here to initialize *****************************/override init (frame:cgrect) {Super.init (frame:  FRAME)//Initialize Lab = UILabel () lab.textalignment =. Center Lab.font = Uifont.systemfont (ofsize: Self.addsubview (Lab) BTN = UIButton () Self.addsubview (BTN)}/************ set the location of the child controls * * * * /override Func Layoutsubviews () {super.layoutsubviews ()//Set child control frame, can also be Here use automatic layout lab.frame = CGRect (X:10, Y:10, width:100, height:40) btn.frame = CGRect (x:lab.frame.origin.x, Y:lab     . Frame.maxy +, width:100, height:40)}/************ incoming model sub-controls to configure, take up NSObject *****************************/     Func setUp (model:nsobject) {lab.text = "Hello"//model.xx btn.settitle ("OK", for:. Normal)//title:model.xx } Required init? (Coder Adecoder:nscoder) {FATalerror ("Init (coder:) have not been implemented")}} 
2. Using a custom control
        //纯代码 view        let view = CustomView()        view.frame = CGRect(x:10, y:100, width:200, height:100)        view.backgroundColor = UIColor.cyan        view.setUp(model: "" as NSObject)        self.view.addSubview(view)

Swift Pure Code custom control

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.