UIView作为子视图添加到应用程序窗口时,设备旋转时不会旋转

时间:2016-10-20 10:44:56

标签: ios swift uiview rotation

我创建了UIView的扩展程序并创建了ActivityIndicatorView,并将其作为子视图添加到UIApplication Window。现在,当设备旋转时,UIViewController也会旋转而不是ActivityIndicatorView

internal extension UIView{

func showActivityViewWithText(text: String?) -> UIView{

    let window = UIApplication.sharedApplication().delegate?.window!!
    let baseLineView = window!.viewForBaselineLayout()

    let locView = UIView(frame:window!.frame)
    locView.backgroundColor = UIColor.clearColor()
    locView.center = window!.center

    baseLineView.addSubview(locView)
    baseLineView.bringSubviewToFront(locView)

    let overlay = UIView(frame: locView.frame)
    overlay.backgroundColor = UIColor.blackColor()
    overlay.alpha = 0.35

    locView.addSubview(overlay)
    locView.bringSubviewToFront(overlay)

    let hud = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
    hud.hidesWhenStopped = true
    hud.center = CGPoint(x: locView.frame.size.width/2,
                         y: locView.frame.size.height/2)

    hud.transform = CGAffineTransformMakeScale(1, 1)
    hud.color = UIColor.redColor()
    hud.startAnimating()

    locView.addSubview(hud)
    locView.bringSubviewToFront(hud)
}

1 个答案:

答案 0 :(得分:2)

可能是问题是错过自动修复面膜?尝试添加:

hud.autoresizingMask = [ .flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin ]

我想hud locView autoresizingMasklocView的子视图的原因我认为是... TimeSpan total = new TimeSpan(0); total.Add(timeTaken1); total.Add(timeTaken2); total.Add(timeTaken3); ...