1

我最近注意到我的应用程序中有一个非常奇怪的行为。为视图设置框架时,它的行为就好像原点是我输入的两倍。其他视图不受影响,受影响视图的宽度和高度也不受影响。有人以前见过或听说过这样的事情吗?

编辑:

这是我的自定义 init 方法中的代码,它似乎以某种方式导致了问题,但我无法找出原因。

if ((self = [super initWithFrame:frame])) {
        self.backgroundColor = [UIColor clearColor];
        self.controller = aController;
        self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
        UIView *centerView = [[UIView alloc] initWithFrame:frame];
        [self addSubview:centerView];
        UIToolbar *navBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 44)];
        NSMutableArray *array = [[NSMutableArray alloc] init];
        UIBarButtonItem *item = item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(closeClicked:)];
        [array addObject:item];
        item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [array addObject:item];
        item = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStylePlain target:nil action:nil];
        [array addObject:item];
        item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [array addObject:item];
        item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
        [array addObject:item];
        [navBar setItems:array];
        [centerView addSubview:navBar];
        centerView.clipsToBounds = YES;
        centerView.opaque = TRUE;
        centerView.alpha = 1.0;
        centerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:centerView];
    }
    return self;
}
4

1 回答 1

2

我以前用 xibs 经历过这样奇怪的事情。它通常是由 xib 在内部被标记为 iPhone xib 而不是 iPad xib 引起的。通常它需要我从头开始重新创建 xib。

它是通用应用程序吗?确保您的 iPad xib 是在您的视图控制器初始化时加载的那个。

添加代码后编辑:

问题似乎出在您传入的上。如果您的 init 方法通过了 (10,10,w,h) 的帧,那么当您创建 centerView 时,这意味着它将在 10,10 内上下文是,因此,如果您的封闭视图位于 10,10,并且中心视图位于封闭视图内的 10,10,则中心视图将在更大范围内显示为 20,20。

你会想用 0,0 作为它的 x,y 创建你的 centerView:

CGRect centerViewFrame = CGRectMake(0, 0, frame.size.width, frame.size.height);
UIView *centerView = [[UIView alloc] centerViewFrame];
于 2011-02-03T03:40:22.850 回答