Mathews Mathews - 1 month ago 15
iOS Question

UIView added as subview to the Application Window does not rotate when the device rotates

I had created an extension to the

UIView
and created an
ActivityIndicatorView
and added it as the subview to
UIApplication
Window
. Now when the device rotates the
UIViewController
also rotates and not this
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)
}

Answer

May be problem is in missed autoresizing mask? Try to add:

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

In a reason your hud is subview of a locView autoresizingMask is required for locView too I suppose.