如何在方向改变时显示视图是否迅速?

时间:2016-02-25 11:39:36

标签: ios xcode swift

我希望在纵向模式下显示一个视图,在横向模式下显示另一个视图。可能吗?我试过这段代码,但视图重叠。如何解决?

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    let app = UIApplication.sharedApplication()

    if (app.statusBarOrientation.isLandscape) 

        var imageView : UIImageView
        imageView  = UIImageView(frame: CGRect(x: 0, y: 0, width: 1300
            , height: 50))
        imageView.contentMode = UIViewContentMode.ScaleToFill
        //        UIGraphicsBeginImageContext(self.view.frame.size)
        imageView.image = UIImage(named:"Header")
        self.view.addSubview(imageView)

        UIGraphicsBeginImageContext(self.view.frame.size)
        UIImage(named: "Landscape")?.drawInRect(self.view.bounds)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.view.backgroundColor = UIColor(patternImage: image)
       display1()

    if (app.statusBarOrientation.isPortrait)

        var imageView : UIImageView
        imageView  = UIImageView(frame: CGRect(x: 0, y: 0, width: 800
            , height: 50))
        imageView.contentMode = UIViewContentMode.ScaleToFill
        //        UIGraphicsBeginImageContext(self.view.frame.size)
        imageView.image = UIImage(named:"Header")
        self.view.addSubview(imageView)

        UIGraphicsBeginImageContext(self.view.frame.size)
        UIImage(named: "background")?.drawInRect(self.view.bounds)
        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        self.view.backgroundColor = UIColor(patternImage: image)
        print(String.self, "sandeep potrait")
       displayKeyboard()

1 个答案:

答案 0 :(得分:2)

您需要隐藏或删除您不希望以当前方向查看的视图。

为此,您需要为这些视图创建属性,例如:

var portraitImageView: UIImageView?
var landscapeImageView: UIImageView?

func ... {
    if (app.statusBarOrientation.isLandscape) {
        var imageView: UIImageView
        ...
        self.view.addSubview(imageView)

        self.landscapeImageView = imageView

        self.portraitImageView.removeFromSuperview()
        self.portraitImageView = nil
    } else {
        ...
    }
}