简体   繁体   English

在所有ViewControllers Swift iOS 11中添加背景图像的最佳实践

[英]Best practices to add background image in all ViewControllers swift ios 11

Previously on ios 10 I hade the following code which works perfect for me. 以前在ios 10上,我有以下代码对我来说很完美。 I just added UIView with UIImage + UIView with specific colour. 我刚刚添加了UIView和具有特定颜色的UIImage + UIView。 After testing it on ios 11 I found out that some of the screens have this ImageView on top of the rest UI elements, so whats wrong with that 在ios 11上对其进行测试后,我发现某些屏幕在其余UI元素的顶部都具有此ImageView,所以这有什么问题呢?

 override func viewDidLoad() {
    super.viewDidLoad()

    configureView()
}

func configureView() {
    view.backgroundColor = UIColor.clear
    let backgroundImage = getBackgroundImage()
    view.addSubview(backgroundImage)
    view.sendSubview(toBack: backgroundImage)
}

func getBackgroundImage() -> UIView {
    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
    backgroundImage.image = UIImage(named: "lemons")
    backgroundImage.contentMode =  UIViewContentMode.scaleAspectFill

    let backgroundView = UIView(frame: UIScreen.main.bounds)
    backgroundView.backgroundColor = black
    backgroundImage.addSubview(backgroundView)

    return backgroundImage
}

If you're creating your view controller programmatically (meaning no storyboard or nib), you can add a background image very simply like this: 如果您以编程方式创建视图控制器(这意味着没有情节提要或笔尖),则可以非常简单地添加背景图片,如下所示:

class SomeViewController: UIViewController {

    override func loadView() {
        view = UIView()
        view.frame = UIScreen.main.bounds
        setBackground()
    }

    func setBackground() {

        // set image
        let bg = UIImageView()
        bg.image = // some UIImage
        bg.frame = view.bounds
        bg.contentMode = .scaleAspectFill
        bg.clipsToBounds = true
        view.insertSubview(bg, at: 0)

        // set tint
        let tint = UIView()
        tint = view.bounds
        tint = UIColor.red
        tint = 0.5
        view.insertSubview(tint, at: 1)

    }

}

Remember, when you create a view controller programmatically, you must create the view controller's actual view, ie view = UIView() . 请记住,以编程方式创建视图控制器时,必须创建视图控制器的实际视图,即view = UIView() Furthermore, "core" tasks, like setting a background image, should be done in loadView and "finishing-touch" tasks, or tasks that you want to be executed after the view controller has loaded itself entirely into memory, should be executed in viewDidLoad . 此外,“核心”任务(如设置背景图像)应在loadView和“ finishing-touch”任务中完成,或者要在视图控制器将自身完全加载到内存中后执行的任务应在viewDidLoad执行。 And, of course, configuring the view controller's view property should definitely not be left for last. 而且,当然,配置视图控制器的view属性绝对不应留给最后。

Move configureView() call from viewDidLoad into viewWillLayoutSubviews . configureView()调用从viewDidLoad移到viewWillLayoutSubviews The latter is inconvenient in that it is called many times in the course of the view controller's life, so use a Bool property to check, so that you only call configureView() once , the very first time. 后者的不便之处在于它在视图控制器的整个使用过程中被多次调用,因此请使用Bool属性进行检查,以便您第一次只调用一次 configureView()

Swift 4: 斯威夫特4:

Better to keep it in helper class: 最好将其保留在助手类中:

static func setBlurBackGround(_ vc:UIViewController){
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "BackGround")
        imageView.frame = dBounds
        vc.view.addSubview(imageView)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:所有ViewController的默认Storyboard背景图像 - Swift: Default Storyboard background Image for all ViewControllers 在 swift (ios) 中保存图像的最佳实践 - Best Practices for saving images in swift (ios) iOS应用导航,使用Swift的最佳做法 - iOS app navigation, best practices using Swift iOS 10 Swift和定义UI的最佳做法 - iOS 10 Swift and best practices for defining the UI 如何使用Swift将相同的背景图像添加到ios应用程序的所有视图中? - How can I add the same background image to all views in my ios app using swift? 使用Appdelegate在IOS Swift中为所有视图控制器创建Firebase CRUD的方法 - Using Appdelegate to create methods for Firebase CRUD in IOS Swift for all viewcontrollers 从内存中删除所有先前加载的ViewController iOS Swift - Removing all previously loaded ViewControllers from memory iOS Swift 如何在iOS Swift中向多个ViewController添加通用视图? - How can I add a common view to multiple ViewControllers in iOS Swift? Swift:将单个图像添加到UITableView中的所有单元格的最佳方法 - Swift: Best way to add one single image to all cells in a UITableView iOS Swift + Parse.com - 用户身份验证最佳做法 - iOS Swift + Parse.com - user authentication best practices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM