我试图找出如何在Xcode Playgrounds中创建UIViewController并将UIView子类附加到它。 我对快速编码很新,所以遇到了一些麻烦。
class myView : UIView {
var label : UIButton!
var topHeader : UIImageView!
var nameText : UITextField!
var password : UITextField!
override init(frame: CGRect) {
super.init(frame: CGRect(x: 10, y: 10, width: 350, height: 450))
self.backgroundColor = UIColor.white
self.layer.borderColor = UIColor.black.cgColor
self.layer.borderWidth = 0.5
//place matters what gets loaded in first.
header("")
loginButton()
nameyText()
nameyText2()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
convenience init() {
self.init(frame: CGRect.zero)
}
func header(_ imageInsert: String){
let topHeader = UIImageView()
topHeader.frame = CGRect(x: -1, y: -1, width: 400, height: 125)
topHeader.backgroundColor = UIColor.white
topHeader.layer.borderWidth = 0.5
topHeader.translatesAutoresizingMaskIntoConstraints = true
topHeader.image = UIImage(named: imageInsert)
self.addSubview(topHeader)
}
func loginButton (){
//create properties for button
let label = UIButton(type: UIButtonType.roundedRect)
label.backgroundColor = .white
label.frame = CGRect(x: 135, y: 315, width: 75, height: 30)
label.setTitle("Login", for: UIControlState.normal)
label.layer.borderWidth = 0.5
label.layer.borderColor = UIColor.blue.cgColor
label.layer.cornerRadius = 4
self.addSubview(label)
}
func nameyText () {
let nameText = UITextField()
nameText.frame = CGRect(x: 45, y: 200, width: 250, height: 30)
nameText.backgroundColor = .white
nameText.borderStyle = .roundedRect
nameText.layer.borderColor = UIColor.lightGray.cgColor
nameText.placeholder = "Username"
nameText.keyboardAppearance = .default
self.addSubview(nameText)
}
func nameyText2 () {
let password = UITextField()
password.frame = CGRect(x: 45, y: 255, width: 250, height: 30)
password.backgroundColor = .white
password.borderStyle = .roundedRect
password.layer.borderColor = UIColor.lightGray.cgColor
password.placeholder = "Password"
password.keyboardAppearance = .default
password.isSecureTextEntry = true
self.addSubview(password)
}
}
这是我创建的UIView子类,看起来像这样...... UIView
然后我创建了我的主要UIViewController类,将其添加到...
import UIKit
import PlaygroundSupport
public class ViewController : UIViewController, UITextFieldDelegate {
public override func viewDidLoad() {
super.viewDidLoad()
}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public override func loadView() {
//trying to make myView the root view for This ViewController
self.view = myView()
}
}
PlaygroundPage.current.liveView = ViewController()
但是,当我将UIView子类添加到UIViewController时,它看起来像这样...... UIViewController
这是一个不同的大小,改变我的UIView框架大小什么都不做。
希望有人帮助理解为什么会这样做,谢谢。
答案 0 :(得分:0)
您需要设置视图控制器的.preferredContentSize
。您可以明确地或通过覆盖它来执行此操作:
public class ViewController : UIViewController, UITextFieldDelegate {
override public var preferredContentSize: CGSize {
get { return self.view.bounds.size }
set { super.preferredContentSize = newValue }
}
public override func viewDidLoad() {
super.viewDidLoad()
}
override public func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public override func loadView() {
//trying to make myView the root view for This ViewController
self.view = myView()
}
}