菜鸟教程小白 发表于 2022-12-11 19:51:22

ios - 以编程方式在 Xcode Playgrounds 中创建 UIView 子类


                                            <p><h2>Xcode Playgrounds 中的 UIViewController 和 UIView 类</h2>

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

<pre><code>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(&#34;&#34;)
    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(&#34;Login&#34;, 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 = &#34;Username&#34;
    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 = &#34;Password&#34;
    password.keyboardAppearance = .default
    password.isSecureTextEntry = true

    self.addSubview(password)
}

}
</code></pre>

<p>这是我创建的 UIView 子类,看起来像这样...
<a href="/image/7kIVc.png" rel="noreferrer noopener nofollow">UIView</a> </p>

<p>然后我创建了我的主 UIViewController 类以将其添加到...</p>

<pre><code>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()
</code></pre>

<p>但是,当我将 UIView 子类添加到 UIViewController 时,它看起来像这样... <a href="/image/kjl0K.png" rel="noreferrer noopener nofollow">UIViewController</a> </p>

<p>这是一个不同的大小,改变我的 UIView 框架大小没有任何作用。</p>

<p>希望有人帮助理解它为什么会这样,谢谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要设置 ViewController 的<code>.preferredContentSize</code>。您可以显式执行此操作,也可以覆盖它:</p>

<pre><code>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()
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 以编程方式在 Xcode Playgrounds 中创建 UIView 子类,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49263442/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49263442/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 以编程方式在 Xcode Playgrounds 中创建 UIView 子类