How do you make a background image scale to screen size in swift?

前端 未结 9 2084
刺人心
刺人心 2020-11-27 04:27

I\'m trying to make a UIView image for my background in swift using pattern image. The code I have works well except for the fact that I want the image to take the whole scr

9条回答
  •  离开以前
    2020-11-27 04:57

    Here are your options for scaling!

    For the .contentMode property:

    ScaleToFill This will scale the image inside the image view to fill the entire boundaries of the image view.

    ScaleAspectFit This will make sure the image inside the image view will have the right aspect ratio and fit inside the image view’s boundaries.

    ScaleAspectFill This will make sure the image inside the image view will have the right aspect ratio and fill the entire boundaries of the image view. For this value to work properly, make sure that you have set the clipsToBounds property of the imageview to true.

    class SecondViewController : UIViewController {
    
        let backgroundImage = UIImage(named: "centralPark")
        var imageView: UIImageView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.thirdChoiceField.delegate = self
            self.datePicker.minimumDate = NSDate()
            imageView = UIImageView(frame: view.bounds)
            imageView.contentMode = .ScaleAspectFill
            imageView.clipsToBounds = true
            imageView.image = backgroundImage
            imageView.center = view.center
            view.addSubview(imageView)
            self.view.sendSubviewToBack(imageView)
    

提交回复
热议问题