tadams22
tadams22

Reputation: 37

Swift: Can't create UIImageView from ViewController.swift

I have set up my UIImageView using the below setup. I am trying to draw on the view but can not get anything to appear. I am new to Xcode and want my view to automatically change depending on what the screen size is. When I have used the storyboard I could not figure out how to get the view to change with the screen size and/or rotation. As a result I figured it would be easier to make that happen in the ViewController.swift file. When the program wasn't working I tried to see if the view was appearing on the screen. When I made one of the views red, I still could not see anything on the screen. I am very confused. I am open to any feedback. I appreciate you taking time to read this and am looking forward to assistance.

Thanks,

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

}

Upvotes: 0

Views: 123

Answers (1)

CoolPenguin
CoolPenguin

Reputation: 1235

I think the problem is you have to create the imageview in viewDidAppear if you're setting the screen size, because the screen size hasn't been set and orientated where you're setting it. Try the below and hope this issue gets resolved soon :)

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

}

Upvotes: 0

Related Questions