ios 如何以编程方式快速创建具有控件文本字段、按钮等的自定义视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29030426/
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
How to create custom view programmatically in swift having controls text field, button etc
提问by Anish Parajuli ?
I am trying to access the MyCustomView
from another class using the following code in ViewController.swift ..
我正在尝试MyCustomView
使用 ViewController.swift 中的以下代码从另一个类访问..
var view = MyCustomView(frame: CGRectZero)
.. in the viewDidLoad
method. The problem is the view does not get initialized in the simulator.
.. 在viewDidLoad
方法中。问题是视图没有在模拟器中初始化。
I have already set class in storyboard for the current ViewController.
我已经在故事板中为当前的 ViewController 设置了类。
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)
}
回答by Stefan Arentz
The CGRectZero
constant is equal to a rectangle at position (0,0)
with zero width and height. This is fine to use, and actually preferred, if you use AutoLayout, since AutoLayout will then properly place the view.
该CGRectZero
常数等于在位置的矩形(0,0)
具有零宽度和高度。如果您使用 AutoLayout,这很好用,并且实际上是首选,因为 AutoLayout 将正确放置视图。
But, I expect you do not use AutoLayout. So the most simple solution is to specify the size of the custom view by providing a frame explicitly:
但是,我希望您不要使用 AutoLayout。所以最简单的解决方案是通过显式提供框架来指定自定义视图的大小:
customView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 200, height: 50))
self.view.addSubview(customView)
Note that you also need to use addSubview
otherwise your view is not added to the view hierarchy.
请注意,您还需要使用addSubview
否则您的视图不会添加到视图层次结构中。
回答by David Seek
Swift 3 / Swift 4Update:
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)
回答by Sateesh Pasala
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)
}
回答by Pavel Gatilov
view = MyCustomView(frame: CGRectZero)
In this line you are trying to set empty rect for your custom view. That's why you cant see your view in simulator.
在这一行中,您尝试为自定义视图设置空矩形。这就是为什么你不能在模拟器中看到你的视图。
回答by Pankaj Jangid
let viewDemo = UIView()
viewDemo.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
self.view.addSubview(viewDemo)