Swift:无法从ViewController.swift创建UIImageView

时间:2016-07-05 17:06:31

标签: xcode uiimageview viewcontroller viewdidload uiscreen

我使用以下设置设置了我的UIImageView。我试图在视图上绘制,但无法得到任何东西。我是Xcode的新手,希望我的视图根据屏幕大小自动更改。当我使用故事板时,我无法弄清楚如何通过屏幕大小和/或旋转来改变视图。结果我认为在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

}

1 个答案:

答案 0 :(得分:0)

我认为问题是如果你要设置屏幕尺寸,你必须在viewDidAppear中创建imageview,因为屏幕尺寸没有设置和定位在你设置它的地方。试试下面的内容,希望很快就能解决这个问题:)

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

}