如何在 swift 中以编程方式创建具有控件文本字段、按钮等的自定义视图
How to create custom view programmatically in swift having controls text field, button etc
我正在尝试使用 ViewController.swift 中的以下代码从另一个 class 访问 MyCustomView
..
var view = MyCustomView(frame: CGRectZero)
.. 在 viewDidLoad
方法中。问题是视图没有在模拟器中初始化。
我已经在故事板中为当前 ViewController 设置了 class。
class MyCustomView: UIView {
var label: UILabel = UILabel()
var myNames = ["dipen","laxu","anis","aakash","santosh","raaa","ggdds","house"]
override init(){
super.init()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.addCustomView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addCustomView() {
label.frame = CGRectMake(50, 10, 200, 100)
label.backgroundColor=UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = "test label"
label.hidden=true
self.addSubview(label)
var btn: UIButton = UIButton()
btn.frame=CGRectMake(50, 120, 200, 100)
btn.backgroundColor=UIColor.redColor()
btn.setTitle("button", forState: UIControlState.Normal)
btn.addTarget(self, action: "changeLabel", forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(btn)
var txtField : UITextField = UITextField()
txtField.frame = CGRectMake(50, 250, 100,50)
txtField.backgroundColor = UIColor.grayColor()
self.addSubview(txtField)
}
view = MyCustomView(frame: CGRectZero)
在这一行中,您尝试为自定义视图设置空矩形。这就是为什么您无法在模拟器中看到您的视图。
CGRectZero
常量等于位置 (0,0)
处宽度和高度为零的矩形。如果您使用 AutoLayout,这很好用,而且实际上是首选,因为 AutoLayout 将正确放置视图。
但是,我希望您不要使用 AutoLayout。所以最简单的解决方案是通过显式提供框架来指定自定义视图的大小:
customView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
self.view.addSubview(customView)
请注意,您还需要使用 addSubview
,否则您的视图不会添加到视图层次结构中。
Swift 3 / Swift 4 更新:
let screenSize: CGRect = UIScreen.main.bounds
let myView = UIView(frame: CGRect(x: 0, y: 0, width: screenSize.width - 10, height: 10))
self.view.addSubview(myView)
var customView = UIView()
@IBAction func drawView(_ sender: AnyObject) {
customView.frame = CGRect.init(x: 0, y: 0, width: 100, height: 200)
customView.backgroundColor = UIColor.black //give color to the view
customView.center = self.view.center
self.view.addSubview(customView)
}
let viewDemo = UIView()
viewDemo.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
self.view.addSubview(viewDemo)
我正在尝试使用 ViewController.swift 中的以下代码从另一个 class 访问 MyCustomView
..
var view = MyCustomView(frame: CGRectZero)
.. 在 viewDidLoad
方法中。问题是视图没有在模拟器中初始化。
我已经在故事板中为当前 ViewController 设置了 class。
class MyCustomView: UIView {
var label: UILabel = UILabel()
var myNames = ["dipen","laxu","anis","aakash","santosh","raaa","ggdds","house"]
override init(){
super.init()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.addCustomView()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addCustomView() {
label.frame = CGRectMake(50, 10, 200, 100)
label.backgroundColor=UIColor.whiteColor()
label.textAlignment = NSTextAlignment.Center
label.text = "test label"
label.hidden=true
self.addSubview(label)
var btn: UIButton = UIButton()
btn.frame=CGRectMake(50, 120, 200, 100)
btn.backgroundColor=UIColor.redColor()
btn.setTitle("button", forState: UIControlState.Normal)
btn.addTarget(self, action: "changeLabel", forControlEvents: UIControlEvents.TouchUpInside)
self.addSubview(btn)
var txtField : UITextField = UITextField()
txtField.frame = CGRectMake(50, 250, 100,50)
txtField.backgroundColor = UIColor.grayColor()
self.addSubview(txtField)
}
view = MyCustomView(frame: CGRectZero)
在这一行中,您尝试为自定义视图设置空矩形。这就是为什么您无法在模拟器中看到您的视图。
CGRectZero
常量等于位置 (0,0)
处宽度和高度为零的矩形。如果您使用 AutoLayout,这很好用,而且实际上是首选,因为 AutoLayout 将正确放置视图。
但是,我希望您不要使用 AutoLayout。所以最简单的解决方案是通过显式提供框架来指定自定义视图的大小:
customView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
self.view.addSubview(customView)
请注意,您还需要使用 addSubview
,否则您的视图不会添加到视图层次结构中。
Swift 3 / Swift 4 更新:
let screenSize: CGRect = UIScreen.main.bounds
let myView = UIView(frame: CGRect(x: 0, y: 0, width: screenSize.width - 10, height: 10))
self.view.addSubview(myView)
var customView = UIView()
@IBAction func drawView(_ sender: AnyObject) {
customView.frame = CGRect.init(x: 0, y: 0, width: 100, height: 200)
customView.backgroundColor = UIColor.black //give color to the view
customView.center = self.view.center
self.view.addSubview(customView)
}
let viewDemo = UIView()
viewDemo.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
self.view.addSubview(viewDemo)