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

前端 未结 9 2123
刺人心
刺人心 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 05:05

    I used constraints to make the image "autoLayout". I made a view to show an activity indicator (with full background image), while the view on segue is loading. The code is as follows.

    var containerView: UIView = UIView()
    var actionIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
    
    private func showActivityIndicator() {
        ///first I set the containerView and the background image
        containerView.translatesAutoresizingMaskIntoConstraints = false
        self.view.addSubview(containerView)
        adjustConstFullSize(containerView, parentView: self.view)
        let backImage = UIImageView(image: UIImage(named: "AppBackImage"))
        backImage.contentMode = UIViewContentMode.ScaleAspectFill
        backImage.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(backImage)
        adjustConstFullSize(backImage, parentView: containerView)
    
        ////setting the spinner (activity indicator)
        actionIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0)
        actionIndicator.center = CGPointMake(containerView.bounds.size.width / 2, containerView.bounds.size.height / 2)
        actionIndicator.hidesWhenStopped = true
        actionIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
        containerView.insertSubview(actionIndicator, aboveSubview: backImage)
    
        ///throw the container to the main view
        view.addSubview(containerView)
        actionIndicator.startAnimating()
    }
    

    This is the code for the "adjustConstFullSize" function.

    func adjustConstFullSize(adjustedView: UIView!, parentView: UIView!) {
        let topConstraint = NSLayoutConstraint(item: adjustedView,
            attribute: .Top,
            relatedBy: .Equal,
            toItem: parentView,
            attribute: .Top,
            multiplier: 1.0,
            constant: 0.0)
    
        let leftConstraint = NSLayoutConstraint(item: adjustedView,
            attribute: .Leading,
            relatedBy: .Equal,
            toItem: parentView,
            attribute: .Leading,
            multiplier: 1.0,
            constant: 0.0)
    
        let rightConstraint = NSLayoutConstraint(item: adjustedView,
            attribute: .Trailing,
            relatedBy: .Equal,
            toItem: parentView,
            attribute: .Trailing,
            multiplier: 1.0,
            constant: 0.0)
    
    
        let bottomConstraint = NSLayoutConstraint(item: adjustedView,
            attribute: .Bottom,
            relatedBy: .Equal,
            toItem: parentView,
            attribute: .Bottom,
            multiplier: 1.0,
            constant: 0.0)
    
        parentView.addConstraints([topConstraint, leftConstraint, rightConstraint, bottomConstraint])
    }
    

    In the function shown above, I "tied" the containerView constraints to the main view constraints, making the view "full size". I did the same for the UIImageView and also set the contentMode to AspectFill - this is crucial, because we want the image to fill the content without stretching.

    To remove the view, after the lazy loading, just use the code below.

    private func hideActivityIndicator() {
        actionIndicator.stopAnimating()
        containerView.removeFromSuperview()
    }
    

提交回复
热议问题