我的 activityIndi​​cator 不调整大小

My activityIndicator don't resize

我的登录功能有一个 activityIndi​​cator,但是当我旋转我的设备时,我的 activityIndi​​cator 没有调整大小。 这是我的登录功能代码,我称之为。

@IBAction func login(sender: AnyObject) {
    var valid: Bool = false
    self.viewUtils.showActivityIndicator(self.view)
    username =  userField.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    password = passwordTextField.text.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())


        if (self.username.isEmpty || self.password.isEmpty){
            self.showAlert("Inserte usuario password")
        }
        else{

            var user = UserFunction()
            dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)){
                valid = user.userValid(self.username,password: self.password)

                dispatch_async(dispatch_get_main_queue()){
                    if valid {
                        self.performSegueWithIdentifier("showTable", sender: self )}
                    else{
                    self.showAlert("usuario incorrecto")

                    }


                }
            }
    }
}

这是我的 showactivity 代码:

func showActivityIndicator(uiView: UIView){
    container.frame = uiView.frame
    container.center = uiView.center
    container.backgroundColor = UIColorFromHex(0xffffff, alpha: 0.3)

    loadingView.frame = CGRectMake(0, 0, 80, 80)
    activityIndicator.autoresizingMask =  UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleRightMargin
    loadingView.center = uiView.center
    loadingView.backgroundColor = UIColorFromHex(0x444444, alpha: 0.7)
    loadingView.clipsToBounds = true
    loadingView.layer.cornerRadius = 10

    activityIndicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
    activityIndicator.center = CGPointMake(loadingView.frame.size.width / 2, loadingView.frame.size.height / 2);

    loadingView.addSubview(activityIndicator)
    container.addSubview(loadingView) 
    uiView.addSubview(container)

    activityIndicator.startAnimating()

}

由于您在没有 NSLayoutConstraint 的情况下编写了上述代码段,最短的解决方案是响应:

- viewWillTransitionToSize:withTransitionCoordinator

故事板解决方案

一个更优雅的解决方案是使用 Storyboard + Autolayout 并且恰好 3 行代码,其中 1 行是可选的:

func showActivityIndicator(uiView: UIView){
    self.view.bringSubviewToFront(loadingView); // Only if covered
    loadingView.hidden = false
    activityIndicator.startAnimating()
}

您可以将圆角、颜色等移动到 -viewDidLoad

loadingView.clipsToBounds = true
loadingView.layer.cornerRadius = 10

以这种方式为您的 activityIndi​​cator 定义布局约束:

activityIndicator.translatesAutoresizingMaskIntoConstraints = NO;    
[activityIndicator addConstraint:[NSLayoutConstraint constraintWithItem: activityIndicator
                                 attribute:NSLayoutAttributeCenterX
                                 relatedBy:NSLayoutRelationEqual
                                    toItem: container
                                 attribute:NSLayoutAttributeCenterX
                                multiplier:1.f constant:0.f]]];

[activityIndicator addConstraint:[NSLayoutConstraint constraintWithItem: activityIndicator
                             attribute:NSLayoutAttributeCenterY
                             relatedBy:NSLayoutRelationEqual
                                toItem: container
                             attribute:NSLayoutAttributeCenterY
                            multiplier:1.f constant:0.f]]];