iOS swift使用xib绘制UIView
ios 绘制 swift uiview 使用
2023-09-11 14:17:58 时间
目标:用xib绘制一个UIView,在某个ViewController中调用。
三个文件:ViewController.Swift DemoView.swift DemoView.xib
首先,可以专心将DemoView.xib画出来,别忘记DemoView.xib中UIView的一处设置
然后,写DemoView.swift文件,代码如下:
- class CoreView: UIView {
- //MARK:
- //MARK: properties
- @IBOutlet weak var makefriendsBtn: UIButton!
- @IBOutlet weak var networkBtn: UIButton!
- @IBOutlet weak var everyoneBtn: UIButton!
- //MARK:
- //MARK: constraints
- @IBOutlet weak var makefriendsBtnWidth: NSLayoutConstraint!
- @IBOutlet weak var networkBtnWidth: NSLayoutConstraint!
- @IBOutlet weak var everyoneBtnWidth: NSLayoutConstraint!
- //MARK:
- //MARK: functions
- required init(coder aDecoder: NSCoder) {
- super.init(coder: aDecoder)
- }
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- override func drawRect(rect: CGRect) {
- makeupUI()
- }
- func makeupUI() {
- self.layer.masksToBounds = true
- self.layer.cornerRadius = 3
- makefriendsBtn.layer.borderWidth = 1
- makefriendsBtn.layer.cornerRadius = 3
- makefriendsBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
- makefriendsBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
- makefriendsBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)
- networkBtn.layer.borderWidth = 1
- networkBtn.layer.cornerRadius = 3
- networkBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
- networkBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
- networkBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)
- everyoneBtn.layer.borderWidth = 1
- everyoneBtn.layer.cornerRadius = 0
- everyoneBtn.layer.borderColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1).CGColor
- everyoneBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
- everyoneBtn.addTarget(self, action: "buttonSelected:", forControlEvents: UIControlEvents.TouchUpInside)
- makefriendsBtnWidth.constant = (self.frame.width - 32 - 29) / 3 + 10
- networkBtnWidth.constant = (self.frame.width - 32 - 29) / 3 + 2
- everyoneBtnWidth.constant = (self.frame.width - 32 - 29) / 3 - 2
- }
- func buttonSelected(button: UIButton) {
- button.selected = !button.selected
- if button.selected == true {
- button.backgroundColor = UIColor(red: 107/256, green: 167/256, blue: 249/256, alpha: 1)
- } else {
- button.backgroundColor = UIColor.whiteColor()
- }
- }
- }
下面就可以在ViewController.swift中调用了:
- var myView = NSBundle.mainBundle().loadNibNamed("DemoView", owner: nil, options: nil).first as? DemoView
- myView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.width-50, height: self.view.frame.height-140)
- myView?.center = self.view.center
- if myView != nil {
- self.view.addSubview(myView!)
- }
相关文章
- iOS 使用drawRect: 绘制虚线椭圆
- swift语言注册非免费苹果账号iOS游戏框架Sprite Kit基础教程
- 【AGC】iOS的applink的回调没反应问题分析
- iOS:关于A页面跳转到B页面,B页面跳转到C页面,点击C页面直接返回到A页的几种方法
- iOS利用drawRect方法绘制图形
- Hello_IOS ios开发transform属性
- iOS开发UI篇—ios应用数据存储方式(归档)
- iOS上文本绘制的几种方法
- SwiftUI 网络请求组件之 使用Combin观察iOS应用程序网络连接状态(中文教程含视频源吗)
- SwiftUI popover 快速应用小代码(支持iPad和iOS))
- iOS动画的要素:CALayer维护数据模型和图片,沟通了CPU和GPU--视图中与图形绘制相关的功能
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)
- iOS开发UI篇—ios应用数据存储方式(归档)
- iOS性能优化
- 《iOS应用软件设计之道》—— 2.3 绘制草图的工具
- 《iOS应用软件设计之道》—— 2.8 绘制界面草图
- IOS 被拒 关于 iPhone running iOS 10.3.1 on Wi-Fi connected to an IPv6 network.
- JS IOS/iPhone的Safari浏览器不兼容Javascript中的Date()问题的解决方法
- IOS应用开发-真机测试
- iOS开发-聊天气泡的绘制和聊天消息列表
- Mac机获取IOS手机udid