ios 自定义 UIView 类 - Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33935566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom UIView Class - Swift
提问by Nata Mio
I've builded a view which appears from the bottom and hides after sometime and it works well,but i want to make it in UIView
class as a modal, i looked over the internet but i couldn't get or understand how to do that.
我已经建立了一个从底部出现并在一段时间后隐藏的视图,它运行良好,但我想在UIView
课堂上将它作为模态,我查看了互联网,但我无法获得或理解如何做到这一点。
snake = UIView(frame: CGRect(x: 0 , y: self.view.frame.size.height-66, width: self.view.frame.size.width, height: 66))
snake.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
let label = UILabel(frame: CGRect(x: 12, y: 8, width: snake.frame.size.width-90, height: 50))
label.text = "Connection error please try again later!!"
label.textColor = UIColor.whiteColor()
label.numberOfLines = 0
label.font = UIFont.systemFontOfSize(14)
snake.addSubview(label)
let button = UIButton(frame: CGRect(x: snake.frame.size.width-87, y: 8, width: 86, height: 50))
button.setTitle("OK", forState: UIControlState.Normal)
button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal)
button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside)
snake.addSubview(button)
self.view.addSubview(snake)
How to start with this class I've no idea, i am creating the view programmatically with its frame and i need to set properties for the button for example or the label and create the view from any class.
如何从这个类开始我不知道,我正在用它的框架以编程方式创建视图,我需要为按钮或标签设置属性,并从任何类创建视图。
class snake: UIView {
override init (frame : CGRect) {
super.init(frame : frame)
}
convenience init () {
self.init(frame:CGRect.zero)
}
required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
}
回答by Nguyen Hoan
code:
代码:
var label:UILabel!
var button:UIButton!
override init (frame : CGRect) {
super.init(frame : frame)
self.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0)
label = UILabel(frame: CGRect(x: 12, y: 8, width: self.frame.size.width-90, height: 50))
label.text = "Connection error please try again later!!"
label.textColor = UIColor.whiteColor()
label.numberOfLines = 0
label.font = UIFont.systemFontOfSize(14)
self.addSubview(label)
button = UIButton(frame: CGRect(x: self.frame.size.width-87, y: 8, width: 86, height: 50))
button.setTitle("OK", forState: UIControlState.Normal)
button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal)
button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(button)
}
and use it:
并使用它:
let snakeView = snake(frame: CGRectMake(0 ,self.view.frame.size.height-66, self.view.frame.size.width, 66)))
and set data for snakeview:
并为蛇视图设置数据:
snakeView.label.text = "hello"
But usually i 'll create a function to update data for view:
但通常我会创建一个函数来更新视图数据:
func updateData(title:String){
self.label.text = title
}
and call it when need:
并在需要时调用它:
snake.updateData("hello")
P/s: if you use xib, you must to implement awakeFromNib instead of init. And create snake with xib(remember set identifier for xib : "snakeView"):
P/s:如果你使用xib,你必须实现awakeFromNib 而不是init。并使用 xib 创建蛇(记住为 xib 设置标识符:“snakeView”):
let snakeView = NSBundle.mainBundle().loadNibNamed("snakeView", owner: nil, options: nil)[0] as snakeView