【问题标题】:Swift: Can't create UIImageView from ViewController.swiftSwift:无法从 ViewController.swift 创建 UIImageView
【发布时间】:2016-07-05 17:06:30
【问题描述】:

我已经使用以下设置设置了我的 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

}

【问题讨论】:

  • 所以要清楚,您希望 UIImageView 调整到屏幕大小。
  • 是的。我尝试了上面的代码并在某处犯了错误。当我在情节提要中这样做时,我无法弄清楚如何让它随着屏幕旋转而改变。
  • 我的回答对你有用吗?
  • 当我在 viewdidload 中创建变量时,我无法在整个程序中看到它。当我拉出变量行时,我收到一条错误消息,提示“'NSObject' 类型的值 - > 'ViewController' 没有成员视图。”感谢您的帮助。
  • 立即尝试。我已经更新了答案

标签: xcode uiimageview viewcontroller viewdidload uiscreen


【解决方案1】:

我认为问题在于,如果您正在设置屏幕尺寸,则必须在 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

}

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 2019-08-07
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多