gpt4 book ai didi

ios - 更改方向更改背景失败

转载 作者:搜寻专家 更新时间:2023-10-31 19:31:27 24 4
gpt4 key购买 nike

我有两个图像 bg-landbg-port 用于不同的方向。我想改变改变方向的形象。但是,图像不会改变。当我在 LandScape 中加载模拟器时,bg-land 工作完美但不适用于旋转。代码:

   override func viewDidLoad() {
super.viewDidLoad()
loadBackground()
}

func loadBackground(){

if(UIApplication.sharedApplication().statusBarOrientation.isPortrait){
var bgImage = UIImage(named: "bg-port")
var background = UIImageView(frame: self.view.bounds);
background.image = bgImage
self.view.addSubview(background)
self.view.sendSubviewToBack(background)
println(bgImage)
} else {
var bgImage = UIImage(named: "bg-land")
var background = UIImageView(frame: self.view.bounds);
background.image = bgImage
self.view.addSubview(background)
self.view.sendSubviewToBack(background)
println(bgImage)
}


}

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
loadBackground()
}

最佳答案

这段代码对我来说很好,试试这个:

   override func viewDidLoad() {
super.viewDidLoad()
self.loadBackground()
}
func loadBackground(){

if(UIApplication.sharedApplication().statusBarOrientation.isPortrait){
let imageName = "Texture.jpg"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)

imageView.frame = self.view.bounds
view.addSubview(imageView)
} else {
let imageName = "AppIcon.png"
let image = UIImage(named: imageName)
let imageView = UIImageView(image: image!)

imageView.frame = self.view.bounds
view.addSubview(imageView)
}


}
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
loadBackground()
}

不要忘记在此代码中更改图像名称。

也许这会对你有所帮助。

关于ios - 更改方向更改背景失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27873707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com