[英]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.