服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Swift - Swift代码自定义UIView实现示例

Swift代码自定义UIView实现示例

2021-12-24 14:15Foreveroriginal Swift

这篇文章主要为大家介绍了Swift如何自定义UIView的实现示例代码,有需要的朋友可以借鉴参考下,希望能够有所帮助祝大家多多进步,早日升职加薪

Swift自定义View和OC自定义View的原理都是一样的,重写init()方法或initWithFrame()方法,下面简单说说如何自定义swift UIView

主要是重写init(frame:CGRect)方法,其中required init?(coder aDecoder: NSCoder) 是必不可少的,swift语言强制要求的

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//
//  LoginImgFieldView.swift
//  SmartMilk
//
//  Created by mac on 2017/6/9.
//  Copyright © 2017年 mac. All rights reserved.
//
import UIKit
let G_HEADIMAGE_HEIGHT:CGFloat = 30
class LoginImgFieldView: UIView {
    var headImgView:UIImageView?
    var field:UITextField?
    var line:UILabel?           
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
//    override init(){
//        super.init()
//        setupSubViews()
//    }   
    override init(frame:CGRect){
        super.init(frame: frame)
        setupSubViews()
    }   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }   
    func setupSubViews() {       
        let frame = self.bounds
        //uiimageVIew
        headImgView = UIImageView(frame:CGRect(x:0,y:0,width:G_HEADIMAGE_HEIGHT,height:G_HEADIMAGE_HEIGHT))
        headImgView?.backgroundColor = UIColor.blue
        
        //uiTextField
        field = UITextField(frame:CGRect(x:0,y:0,width:100,height:G_HEADIMAGE_HEIGHT))
//        field?.font = UIFont.systemFont(ofSize: 15)       
        //uiLabel       
        line = UILabel(frame: CGRect(x:0,y:frame.size.height-1,width:frame.size.width,height:1))
        line?.backgroundColor = UIColor.orange       
        self.addSubview(headImgView!)
        self.addSubview(field!)
        self.addSubview(line!)
    }      
    override func layoutSubviews() {
        super.layoutSubviews()       
        let frame = self.bounds
        let imgY = (frame.size.height - G_HEADIMAGE_HEIGHT)/2
        headImgView?.frame = CGRect(x:0,y:imgY,width:G_HEADIMAGE_HEIGHT,height:G_HEADIMAGE_HEIGHT)       
        //field
        let fieldx = G_HEADIMAGE_HEIGHT+5
        let fieldWidth = frame.size.width - fieldx       
        field?.frame = CGRect(x: fieldx, y: imgY, width: fieldWidth, height: G_HEADIMAGE_HEIGHT)       
        //label
        var lineFrame = line?.frame
        lineFrame?.origin.y = frame.size.height - 2
        lineFrame?.size.width = frame.size.width       
        line?.frame = lineFrame!
        
    }
 
}

到此这篇关于Swift代码自定义UIView实现示例的文章就介绍到这了,更多相关Swift自定义UIView内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/moqj_123/article/details/73351098

延伸 · 阅读

精彩推荐