ios - UIView.transitionWithView 破坏加载器的布局

标签 ios objective-c swift animation uiview

我正在尝试在我的应用程序中为 root-view-controller-change 设置动画。交换 View Controller 后,我立即加载第二个 Controller 所需的数据。在加载数据时,我会显示一个加载程序 (MBProgressHUD)。这是我交换 View Controller 的功能:

class ViewUtils {

    class func animateRootViewController(duration: NSTimeInterval, changeToViewController: UIViewController) {
        let window = UIApplication.sharedApplication().delegate?.window?
        if window == nil {
            return
        }
        UIView.transitionWithView(window!,
            duration: duration,
            options: UIViewAnimationOptions.TransitionFlipFromLeft | UIViewAnimationOptions.AllowAnimatedContent,
            animations: {
                window!.rootViewController = changeToViewController
            },
            completion: nil
        )
    }
}

这一切都很好,但有一件事 - 它完全破坏了加载程序。我附上了正在发生的事情的想象:2nd view controller 这是旋转时的第二个 View Controller 。旋转完成后,加载程序看起来很好,微调器和文本补间都在圆角矩形中的正确位置。

我真的不明白为什么会这样,请有人向我解释一下吗?有什么办法可以预防吗?

我显示加载程序的第二个 View Controller 的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    hud = HUD(containingView: view)
    hud.show()

    createBackground()
}

还有我的 HUD 类:

class HUD {

    private var hudBG: UIView!
    private var view: UIView!
    private(set) var isShown = false

    init(containingView: UIView) {
        view = containingView
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func show() {
        if !isShown {
            if(hudBG == nil) {
                hudBG = UIView(frame: CGRectMake(0, 0, view.bounds.width, view.bounds.height))
                hudBG.backgroundColor = UIColor(white: 0, alpha: 0.4)
            }
            view.addSubview(hudBG)
            let hud = MBProgressHUD.showHUDAddedTo(view, animated: true)
            hud.mode = MBProgressHUDModeIndeterminate
            hud.labelText = "Cargando"

            hudBG.alpha = 0

            UIView.animateWithDuration(0.3, animations: { () -> Void in
                self.hudBG.alpha = 1
            })
            isShown = true
        }
    }

    func hide() {
        if isShown {
            UIView.animateWithDuration(0.3, animations: {
                () -> Void in
                self.hudBG.alpha = 0
            }, completion: {
                (b) -> Void in
                self.hudBG.removeFromSuperview()
            })
            MBProgressHUD.hideHUDForView(view, animated: true)
            isShown = false
        }
    }
}

非常感谢任何想法!

最佳答案

您正在将 HUD 添加到尚未正确初始化的 View 中。 如果您从 xib 或 Storyboard加载 View Controller ,则 View 及其 subview 具有从界面加载时的大小。

您必须在将 View 调整为最终大小后添加 HUD。

如果你移动

hud = HUD(containingView: view)
hud.show()

对于 viewDidLayoutSubviews,它应该可以正常工作。

关于ios - UIView.transitionWithView 破坏加载器的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29687477/

相关文章:

ios - 跟踪自上次尝试______以来的时间

ios - UIImagePickerController 相机 View 大小问题

ios - 使用纯色创建多个 UIImage

swift - firebase json反序列化swift

swift - 闭包中的弱引用

ios - 使用 .gesture(LongPressGesture) 打破 Scrollview

ios - 在 ios 中创建用户可配置的 UI

ios - swift :我 super 困惑! UILabel 上的 UIGravityBehavior vs NSTimer vs CGPointMake

ios - 为什么lazy property和didSet会以递归调用结束?

ios - viewLoad 上的 Swift JSON 解析执行