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

时间:2012-04-08 16:20:35

标签: iphone objective-c xcode cocoa-touch uitableview

我的tableView单元格有一个渐变图层

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

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

同样适用于中心有图像的NavigationController,

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];

因此图像在旋转时不会重新定位,我也不知道导航栏的任何“刷新”方法。

1 个答案:

答案 0 :(得分:1)

我必须警告你,我没有对此进行测试,但我希望在设置autoresizingMask时它可以正常工作。对于表格视图单元格:

cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin  | UIViewAutoresizingFlexibleRightMargin;

对于徽标图片视图:

logoimageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin  | UIViewAutoresizingFlexibleRightMargin;