开发者

Adjust frame of UIView after loading it from IB

Update: cause of the issue was found, see below.

I'm feeling a little embarrassed here since the answer to my question is probably dead simple, but I've been struggling with this piece of code for over a day and I can't figure out what's going on, so here goes:

I've created a custom UIView subclass. By now, I've isolated the code to the part that's causing me headaches:

-(id) initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.backgroundColor = [U开发者_高级运维IColor greenColor];
        self.frame = CGRectMake(0, 0, 320, 60);
        NSLog(@"%@", NSStringFromCGRect(self.frame));
    }
    return self;
}

This code is called when my custom view is loaded from a XIB file. This code attempts to resize the view. It does that, and the NSLog statement gives the same CGRect back as I pass to the view. However, whatever value I provide, the rectangle's height is consistently 50 points smaller than it's supposed to be.

If I create the view programmatically with similar code, the rectangle is drawn correctly:

-(id) initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor orangeColor];
        self.frame = CGRectMake(0, 0, 320, 60);
        NSLog(@"%@", NSStringFromCGRect(self.frame));
    }
    return self;
}

It drives me nuts. What could cause this?

As an additional question, what methods could I use to analyze / visualize the dimensions of views once the app is running in the iPhone simulator?

// Update:

I finally found what caused the weird behavior of this UIView subclass. The view controller that loaded this view as its subview was loaded from a Tab Bar, which has a height of 49 points, so that got me thinking. Apparently, the default autoresizing options that are selected in interface builder cause the view to resize, after the view's bounds get reset in my custom initializer method. Setting no autoresize behavior in Interface Builder solves the issue.


I finally found what caused the weird behavior of this UIView subclass. The view controller that loaded this view as its subview was loaded from a Tab Bar, which has a height of 49 points, so that got me thinking. Apparently, the default autoresizing options that are selected in interface builder cause the view to resize, after the view's bounds get reset in my custom initializer method. Setting no autoresize behavior in Interface Builder solves the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜