将视图居中于视图控制器的中心
Centring a view at the centre of view controller
我试图在加载 table 视图时显示动画微调器。
loadingView = [[UIView alloc] initWithFrame:CGRectMake(75, 155, 170, 170)];
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
loadingView.clipsToBounds = YES;
loadingView.layer.cornerRadius = 10.0;
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.frame = CGRectMake(65, 40, activityView.bounds.size.width, activityView.bounds.size.height);
[loadingView addSubview:activityView];
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
loadingLabel.backgroundColor = [UIColor clearColor];
loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.adjustsFontSizeToFitWidth = YES;
loadingLabel.textAlignment = NSTextAlignmentCenter;
loadingLabel.text = @"Loading...";
[loadingView addSubview:loadingLabel];
[self.view addSubview:loadingView];
[activityView startAnimating];
我在 loadingView
中嵌入了 UIActivityIndicatorView
。
我面临的问题是:
- 加载视图不在视图控制器的中心。
- 我试图将加载视图居中到视图控制器的中心,
UIActivityIndicatorView
必须位于加载视图的中心,但它并没有像我期望的那样居中。
当视图控制器布置子视图时,您应该将其居中。
对于视图控制器,viewDidLayoutSubviews
是一个很好的地方:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.activityView.center = CGPointMake(CGRectGetWidth(self.loadingView.frame) / 2, CGRectGetHeight(self.loadingView.frame) / 2);
self.loadingView.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, CGRectGetHeight(self.view.frame) / 2);
}
在 ViewDidLoad 中添加有效的代码:
[self.view addSubview: loadingView];
loadingView .center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, CGRectGetHeight(self.view.frame) / 2);
设置框架:
int width = 170;
loadingView = [[UIView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width/2) - (width)/2, ([UIScreen mainScreen].bounds.size.height/2) - (height)/2, width, width)];
activityView.frame = CGRectMake((loadingView.frame.size.width/2) -(activityView.bounds.size.width/2), (loadingView.frame.size.height/2) - (activityView.bounds.size.height/2) - 11, activityView.bounds.size.width, activityView.bounds.size.height);
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake((loadingView.frame.size.width/2) - 65, loadingView.frame.size.height - 25, 130, 22)];
或
loadingView.center = CGPointMake(([UIScreen mainScreen].bounds.size.width/2), ([UIScreen mainScreen].bounds.size.height/2) );
我试图在加载 table 视图时显示动画微调器。
loadingView = [[UIView alloc] initWithFrame:CGRectMake(75, 155, 170, 170)];
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
loadingView.clipsToBounds = YES;
loadingView.layer.cornerRadius = 10.0;
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityView.frame = CGRectMake(65, 40, activityView.bounds.size.width, activityView.bounds.size.height);
[loadingView addSubview:activityView];
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 115, 130, 22)];
loadingLabel.backgroundColor = [UIColor clearColor];
loadingLabel.textColor = [UIColor whiteColor];
loadingLabel.adjustsFontSizeToFitWidth = YES;
loadingLabel.textAlignment = NSTextAlignmentCenter;
loadingLabel.text = @"Loading...";
[loadingView addSubview:loadingLabel];
[self.view addSubview:loadingView];
[activityView startAnimating];
我在 loadingView
中嵌入了 UIActivityIndicatorView
。
我面临的问题是:
- 加载视图不在视图控制器的中心。
- 我试图将加载视图居中到视图控制器的中心,
UIActivityIndicatorView
必须位于加载视图的中心,但它并没有像我期望的那样居中。
当视图控制器布置子视图时,您应该将其居中。
对于视图控制器,viewDidLayoutSubviews
是一个很好的地方:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.activityView.center = CGPointMake(CGRectGetWidth(self.loadingView.frame) / 2, CGRectGetHeight(self.loadingView.frame) / 2);
self.loadingView.center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, CGRectGetHeight(self.view.frame) / 2);
}
在 ViewDidLoad 中添加有效的代码:
[self.view addSubview: loadingView];
loadingView .center = CGPointMake(CGRectGetWidth(self.view.frame) / 2, CGRectGetHeight(self.view.frame) / 2);
设置框架:
int width = 170;
loadingView = [[UIView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width/2) - (width)/2, ([UIScreen mainScreen].bounds.size.height/2) - (height)/2, width, width)];
activityView.frame = CGRectMake((loadingView.frame.size.width/2) -(activityView.bounds.size.width/2), (loadingView.frame.size.height/2) - (activityView.bounds.size.height/2) - 11, activityView.bounds.size.width, activityView.bounds.size.height);
loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake((loadingView.frame.size.width/2) - 65, loadingView.frame.size.height - 25, 130, 22)];
或
loadingView.center = CGPointMake(([UIScreen mainScreen].bounds.size.width/2), ([UIScreen mainScreen].bounds.size.height/2) );