如何在UITableViewCell中定位UIActivityIndicatorView
问题描述:
我已将此代码添加到UITableViewCell
的UIActivityIndicatorView
。如何在UITableViewCell中定位UIActivityIndicatorView
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIImage *spacer = [UIImage imageNamed:@"spacer"];
UIGraphicsBeginImageContext(CGSizeMake(220, 150));
[spacer drawInRect:CGRectMake(0,0,spinner.frame.size.width,spinner.frame.size.height)];
UIImage* resizedSpacer = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = resizedSpacer;
[cell.imageView addSubview:spinner];
[spinner startAnimating];
当前图像显示在220x150框的左上角。相反,我想把它放在中心位置。我怎样才能做到这一点?我试图改变CGRectMake
上的两个第一个参数,但这并没有改变任何东西。
答
您已经将resizedSpacer
添加为imageView,但实际上并没有对spinner
的框架进行任何操作。你想要类似的东西:
CGSize spinSize = spinner.frame.size;
spinner.frame = CGRectMake((220-spinSize.width)/2., (150-spinSize.height)/2.,spinSize.width, spinSize.height);
[cell.imageView addSubview:spinner];
我不认为涉及spacer
的逻辑是必需的。实际上,你甚至可能根本不想在UIImageView
中主持UIActivityIndicatorView
;为什么不直接将其放置在tableview单元格内?