子视图在应用程序启动时在设备的不同初始方向上采用不同的帧大小

时间:2016-01-05 11:03:04

标签: ios iphone swift uiview uialertview

我正在设计一个自定义警报,我正在使用在场景停靠站中设计的视图,其中包含约束和故事板文件的下拉框链接https://www.dropbox.com/s/dhk5rrme1j2g89g/Main.storyboard?dl=0 -

enter image description here enter image description here

现在我通过以下代码添加视图 -

 @IBAction func forgotpassword(sender: AnyObject)
    {




                                blurEffect = UIBlurEffect(style: .Dark)
                blurEffectView = UIVisualEffectView(effect: blurEffect)
                blurEffectView.frame = self.contentview.bounds
                blurEffectView.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]


                 AlertVieww.frame = CGRectMake(20, (self.navigationController?.navigationBar.frame.origin.y)! + (navigationController?.navigationBar.frame.height)! + 20, blurEffectView.frame.width-40, self.AlertVieww.frame.height)



                AlertVieww.autoresizingMask = [.FlexibleHeight,.FlexibleWidth]





            blurEffectView.addSubview(self.AlertVieww)



          [self.scrollview.setContentOffset(CGPointZero, animated: false)]
          self.navigationController?.navigationBarHidden = true

        UIView.transitionWithView(self.view.window!, duration: 1.0, options: UIViewAnimationOptions.TransitionFlipFromBottom, animations:
            {


                self.contentview.addSubview(self.blurEffectView)



            }, completion: { finished in


                self.scrollview.scrollEnabled = false



        })

    }

我发布以下场景以及我的simmulator的屏幕截图 -

1.当我要触摸警报按钮时,当模拟器处于potrait模式时

potrait中的模拟器 -

enter image description here

然后我将它旋转到风景 -

enter image description here

2.当我要触摸警报按钮时,当模拟器处于横向模式时

横向模拟器 -

enter image description here

然后我把它旋转到potrait ****(这是问题来的时候)** - ** 此外,我的用户与发送和取消按钮的交互也被禁用。

enter image description here

如何解决上述问题?如何将alertview固定在屏幕上的正确位置。 编辑我也尝试过对我的问题https://stackoverflow.com/a/34610391/5395919投票最多的答案,但这并没有解决我的问题。

2 个答案:

答案 0 :(得分:3)

setTranslatesAutoresizingMaskIntoConstraints设为true

来自docs

  

默认情况下,对于您的任何视图,该属性都设置为true   以编程方式创建。如果在Interface Builder中添加视图,则   系统自动将此属性设置为false。

blurEffectView.autoresizingMask = [.FlexibleWidth,.FlexibleHeight]
blurEffectView.setTranslatesAutoresizingMaskIntoConstraints(true) 

AlertVieww.autoresizingMask = [.FlexibleHeight,.FlexibleWidth]
AlertVieww.setTranslatesAutoresizingMaskIntoConstraints(true) 

答案 1 :(得分:0)

使用autlo layout / NSLayoutConstraint作为警报视图。 这可能会对你有帮助。

You need to set the following constraints.

1. centerX and centerY constraints for center your alert view.
2. Add the space constraint in between "send" and "cancel" buttons.
3. Add the top constraint for your label and textfield.

阅读以下最佳教程以便更好地理解。 http://angelovillegas.com/2014/01/31/ios-nslayoutconstraint/