0

我已将此代码添加到UITableViewCellUIActivityIndicatorView如何在UITableViewCell中定位UIActivityIndi​​catorView

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上的两个第一个参数,但这并没有改变任何东西。

回答

3

您已经将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单元格内?

相关问题