基础知识

iOS xib 加载view 修改不了frame的问题

2018-01-15  本文已影响70人  半杯故事

方法一:

@interface AddTextView ()
{
    CGRect myframe;
}
@end



@implementation AddTextView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

        NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:@"AddTextView" owner:nil options:nil];
        self=[nibs objectAtIndex:0];
        myframe = frame;

        NSLog(@"gao =  %f",frame.size.height);
    }
    return self;
}

-(void)drawRect:(CGRect)rect{
    self.frame = myframe;
}
@end

方法二:


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

        UIView *view =  [[[NSBundle mainBundle]loadNibNamed:@"AddTextView" owner:self options:nil] firstObject];
        view.backgroundColor = [UIColor redColor];
        view.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        self.backgroundColor  = [UIColor yellowColor];
        self.frame = frame;
        [self addSubview:view];

    }
    return self;
}

上一篇下一篇

猜你喜欢

热点阅读