草庐IT

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

coder 2024-01-28 原文

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

需要一些帮助来理解它为什么这样做,谢谢。

最佳答案

您需要设置 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/

有关ios - 以编程方式在 Xcode Playgrounds 中创建 UIView 子类的更多相关文章

  1. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  2. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  3. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  4. ruby-on-rails - Rails - 从另一个模型中创建一个模型的实例 - 2

    我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案

  5. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  6. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  7. ruby-on-rails - 正确的 Rails 2.1 做事方式 - 2

    question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参

  8. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  9. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  10. ruby - 如何在 Ruby 中创建无类 DSL? - 2

    我正在尝试找出如何为我的Ruby项目创建一种“无类DSL”,类似于在Cucumber步骤定义文件中定义步骤定义或在Sinatra应用程序中定义路由。例如,我想要一个文件,其中调用了我的所有DSL函数:#sample.rbwhen_string_matches/hello(.+)/do|name|call_another_method(name)end我认为用我的项目特有的一堆方法污染全局(内核)命名空间是一种不好的做法。因此方法when_string_matches和call_another_method将在我的库中定义,并且sample.rb文件将以某种方式在我的DSL方法的上下文中

随机推荐