Search code examples
swiftuiviewuiimageviewuiscreen

Placing an object in respect with screen in swift


I would like to set an image view in respect with screen and not the view. I have tried doing something like the following, but it does not work.

bgImgView = UIImageView(image: bgImage)
var scrFrame = UIScreen.mainScreen().bounds.size
bgImgView.frame = CGRectMake(0, -self.view.frame.origin.y, scrFrame.width, scrFrame.height)
self.view.addSubview(self.bgImgView)

Solution

  • You could add the view to the main app window on top of all other views, although this could cause you issues relating to device orientation.

    bgImgView = UIImageView(image: bgImage)
    bgImgView.backgroundColor = UIColor.redColor()
    var scrFrame = UIScreen.mainScreen().bounds.size
    bgImgView.frame = CGRectMake(0, 0, scrFrame.width, scrFrame.height)
    var appDelegate : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate;
    appDelegate.window?.addSubview(self.bgImgView)
    

    Depending on what you're intending to do with the image view, you could also create a modal view for this purpose (which I'd recommend over the above).