【问题标题】:how to display view when orientation is changed is swift? [duplicate]方向改变时如何快速显示视图? [复制]
【发布时间】:2016-02-25 11:39:36
【问题描述】:

我想在纵向模式下显示一个视图,在横向模式下显示另一个视图。是否可以?我尝试了这段代码,但视图重叠。如何解决?

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()

【问题讨论】:

  • @nhgrif 不,它不是那个链接伙伴的副本,这就是我面临的问题,就像我在纵向模式下创建了 6 个按钮 3X2(行 x 列),它必须在横向模式,但是进入横向模式后发生的事情是,视图控制器覆盖了视图,这意味着在横向方向之后,我可以在一个视图中同时看到 3x2 和 2x3 视图,这很棘手
  • 链接的问题是这个问题的简化版本。

标签: ios xcode swift


【解决方案1】:

您需要隐藏或移除您不想在当前方向看到的视图。

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

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 {
        ...
    }
}

【讨论】:

  • @Anoton Tyutin nope 它不工作任何其他选项请
  • @Sandpie 您面临什么问题?您在else 的情况下正确切换变量吗?
  • @MichaëlAzevedo 我面临的就像我在纵向模式下创建了 6 个按钮 3X2(行 x 列),它必须在横向模式下更改为 2x3,但进入横向模式后会发生什么,查看控制器编写视图,这意味着在横向后我可以在一个视图中同时看到 3x2 和 2x3 视图,这很棘手
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 2010-10-27
  • 1970-01-01
相关资源
最近更新 更多