VFL的子视图居中,网上搜了很多都没找到有用代码,自己研究出来了。代码如下
这样就设置了一个imageview其大小为60*60 在整个视图中居中。
- UIActivityIndicatorView* prgrssView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
- prgrssView.translatesAutoresizingMaskIntoConstraints = NO;
- [self.view addSubview:prgrssView];
- [prgrssView release];
- NSDictionary* views = NSDictionaryOfVariableBindings(prgrssView);
- [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[prgrssView]-|" options:NSLayoutFormatAlignAllCenterX metrics:nil views:views]];
- [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[prgrssView]-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
- //beging
- UIImageView* imagevew = [UIImageView autolayoutView];
- [imagevew setContentMode:UIViewContentModeScaleToFill];
- [imagevew setImage:_tabCellImage];
- [imagevew setTag:AEC_TAG_IMAGE_VIEW];
- [self.view addSubview:imagevew];
- [imagevew release];
- NSDictionary* views = NSDictionaryOfVariableBindings(imagevew);
- //设置高度
- [self.view.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[imagevew(60)]" options:0 metrics:nil views:views]];
- //设置宽度
- [self.view.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"[imagevew(60)]" options:0 metrics:nil views:views]];
- //垂直居中
- [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imagevew attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
- //水平居中
- [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imagevew attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
- //end
这样就设置了一个imageview其大小为60*60 在整个视图中居中。