OStack程序员社区-中国程序员成长平台

标题: ios - 以编程方式在 Xcode Playgrounds 中创建 UIView 子类 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:51
标题: ios - 以编程方式在 Xcode Playgrounds 中创建 UIView 子类

Xcode Playgrounds 中的 UIViewController 和 UIView 类

我试图弄清楚如何在 Xcode Playgrounds 中创建 UIViewController 并将 UIView 子类附加到它。 我对 swift 和编码很陌生,所以遇到了一些麻烦。

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 = "assword"
    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 框架大小没有任何作用。

希望有人帮助理解它为什么会这样,谢谢。



Best Answer-推荐答案


您需要设置 View Controller 的.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()
    }
}

关于ios - 以编程方式在 Xcode Playgrounds 中创建 UIView 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49263442/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4