How to position activity indicator to the center of its superview using Auto Layout programmatically?

后端 未结 4 1800
终归单人心
终归单人心 2021-02-12 12:18

Why the following code does not work for positioning activity indicator to the center of its superview:

UIActivityIndicatorView *activityIndicator = [[UIActivity         


        
4条回答
  •  臣服心动
    2021-02-12 12:50

    You can try this,

     UIView *superview = self.mysuperview;
    NSDictionary *variables = NSDictionaryOfVariableBindings(activityIndicator, superview);
    NSArray *constraints =
    [NSLayoutConstraint constraintsWithVisualFormat:@"V:[superview]-(<=1)-[activityIndicator]"
                                            options: NSLayoutFormatAlignAllCenterX
                                            metrics:nil
                                              views:variables];
    [self.view addConstraints:constraints];
    
    constraints =
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:[superview]-(<=1)-[activityIndicator]"
                                            options: NSLayoutFormatAlignAllCenterY
                                            metrics:nil
                                              views:variables];
    [self.view addConstraints:constraints];
    

    Taken from here.

提交回复
热议问题