如何在旋转时重新定位图层



我的表视图单元格具有渐变层

CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = cell.contentView.bounds;
    gradient.colors = 
    [NSArray arrayWithObjects:
     (id)[[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1] CGColor],
     (id)[[UIColor colorWithRed:0.82 green:0.82 blue:0.82 alpha:1] CGColor], nil];
    [cell.contentView.layer insertSublayer:gradient atIndex:0];

旋转时显然保持在初始尺寸中,直到我调用

tableview.reloadData

我想知道是否有比这更复杂的方法。

导航控制器也是如此,它有一个位于中心的图像,

UIImage *logo = [UIImage imageNamed:@"logo.png"];
UIImageView *logoimageview = 
[[UIImageView alloc] initWithFrame:
 CGRectMake(self.navigationController.navigationBar.bounds.size.width / 2 - logo.size.width / 2, 
            self.navigationController.navigationBar.bounds.size.height / 2 - logo.size.height / 2, 
            logo.size.width, logo.size.height)];
[logoimageview setImage:logo];
[self.navigationController.navigationBar addSubview:logoimageview];

所以图像不会在旋转时重新定位,而且我不知道导航栏有任何"刷新"方法。

我必须警告你,我没有测试这个,但我希望当你设置自动调整大小掩码时它应该可以工作。对于表视图单元格:

cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin  | UIViewAutoresizingFlexibleRightMargin;

对于徽标图像视图:

logoimageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin  | UIViewAutoresizingFlexibleRightMargin;

最新更新