Swift: 无法从 ViewController.swift 创建 UIImageView

Swift: Can't create UIImageView from ViewController.swift

我已经使用以下设置设置了我的 UIImageView。我正在尝试绘制视图,但无法显示任何内容。我是 Xcode 的新手,希望我的视图能够根据屏幕尺寸自动更改。当我使用故事板时,我无法弄清楚如何让视图随着屏幕尺寸 and/or 旋转而改变。因此,我认为在 ViewController.swift 文件中实现这一点会更容易。当程序不工作时,我试图查看视图是否出现在屏幕上。当我将其中一个视图设为红色时,屏幕上仍然看不到任何内容。我很迷茫。我愿意接受任何反馈。感谢您花时间阅读本文,期待您的帮助。

谢谢,

T

import UIKit
import CoreGraphics

class ViewController: UIViewController {

var ResultImageView = UIImageView ( frame: UIScreen.mainScreen().bounds)
var DrawingImageView = UIImageView ( frame: UIScreen.mainScreen().bounds)

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.



    ResultImageView.userInteractionEnabled = true

    ResultImageView.backgroundColor = UIColor.redColor()



    DrawingImageView.userInteractionEnabled = true

}

我认为问题在于,如果您要设置屏幕尺寸,则必须在 viewDidAppear 中创建图像视图,因为屏幕尺寸尚未设置,也未定位到您设置的位置。试试下面的方法,希望这个问题尽快得到解决 :)

var ResultImageView = UIImageView()
var DrawingImageView = UIImageView()
override func viewDidAppear(animated: Bool) {
   super.viewDidAppear(true)
   ResultImageView = UIImageView (frame: self.view.frame)
   DrawingImageView = UIImageView (frame: self.view.frame)
   ResultImageView.userInteractionEnabled = true
   self.view.addSubview(ResultImageView)
   self.view.addSubview(DrawingImageView)
   ResultImageView.backgroundColor = UIColor.redColor()



   DrawingImageView.userInteractionEnabled = true

}