旋转后快速更改视图位置

时间:2016-02-19 02:43:40

标签: ios swift

我有一个应用程序,允许用户在potrait和横向使用。我的目标是有一个像this这样的视图(左边的景观和右边的potrait。我设法使用以下代码重新定位视图

func changeViewAfterRotate(orientation : UIInterfaceOrientation) {
    if (orientation == .LandscapeLeft ||
        orientation == .LandscapeRight)
    {
        headlineImage.frame = CGRectMake(0, 0, self.view.frame.size.width/2 , self.view.frame.size.height)

        let statusbarHeight = UIApplication.sharedApplication().statusBarFrame.size.height
        let navBarHeight = self.navigationController?.navigationBar.frame.size.height
        let viewHeight = self.view.frame.size.height - statusbarHeight - navBarHeight!
        let titleHeight = self.headlineTitleView.frame.size.height + statusbarHeight + navBarHeight!


        let yPos = statusbarHeight + navBarHeight!

        headlineTitleView.frame = CGRectMake(self.view.frame.size.width/2, yPos, self.view.frame.size.width/2 , self.headlineTitleView.frame.size.height)
        headlineScrollView.frame = CGRectMake(self.view.frame.size.width/2, titleHeight, self.view.frame.size.width/2 , viewHeight - titleHeight)

        let fixedHeight = NSLayoutConstraint(item: headlineTitleView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 134)
                    view.addConstraint(fixedHeight)
    }
    else
    {
        headlineImage.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/3)

        print("headlineImage.frame.size.width \(headlineImage.frame.size.width)")

        let titleHeight =  self.headlineImage.frame.size.height

        headlineTitleView.frame = CGRectMake(0, titleHeight , self.view.frame.size.width , self.headlineTitleView.frame.size.height)
        headlineScrollView.frame = CGRectMake(0, titleHeight + self.headlineTitleView.frame.size.height, self.view.frame.size.width , self.view.frame.size.height - titleHeight - self.headlineTitleView.frame.size.height)

        let equalWidth = NSLayoutConstraint(item: headlineScrollView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 1.0, constant: 0)
        view.addConstraint(equalWidth)
    }

我的问题是我是第一次为每个视图轮换IPad,它将如下所示:landscape on the left。我将IPad旋转几次后恢复正常。任何人都可以帮我解决这个问题吗?

0 个答案:

没有答案