Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Swift customizes UIView

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to customize Swift UIView, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

The principles of Swift custom View and OC custom View are the same. Override the init () method or initWithFrame () method. Let's briefly talk about how to customize swift UIView.

It is mainly to rewrite init (frame:CGRect) methods, in which required init? (coder aDecoder: NSCoder) is essential and required by the swift language.

/ LoginImgFieldView.swift// SmartMilk//// Created by mac on 2017 / 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 (XRV 0) ) headImgView?.backgroundColor = UIColor.blue / / uiTextField field = UITextField (frame:CGRect (frame:CGRect)) / / uiLabel line = UILabel (ofSize: 15) / / uiLabel line = UILabel YRV frame. Size.heightlle 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 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!}} these are all the contents of the article "how to customize UIView by Swift" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report