【发布时间】:2014-05-28 09:40:13
【问题描述】:
我有一个自定义的UIView 类
CustomView.m
- (instancetype)initWithString:(NSString *)aString {
self = [super init];
if (!self)
return nil;
[self setAutoresizesSubviews:YES];
UILabel *label = [[UILabel alloc] init];
label.frame = ({
CGRect frame = self.frame;
frame.origin.x = CGRectGetMinX(frame) + 50;
frame.origin.y = CGRectGetMinY(frame) + 20;
frame.size.width = CGRectGetWidth(frame) - 100;
frame.size.height = CGRectGetHeight(frame) - 40;
frame;
});
[label setText:aString];
[self addSubview:label];
return self;
}
ViewController.m
-(void)addCustomView {
CustomView *custom = [CustomView alloc] initWithString:@"abc"];
custom.frame = ({
CGRect frame = self.view.frame;
frame.origin.y = CGRectGetHeight(frame) - 100;
frame.size.height = 100;
frame;
});
[self.view addSubview: custom];
}
这就是问题所在,我在alloc init 之后设置了CustomView 的frame。
这意味着它的框架在我更改之前是CGRectZero。
因此,UILabel 的帧也为零。
在我的子视图的框架改变后如何改变我的子视图的框架?如上。
谢谢。
【问题讨论】:
标签: ios objective-c uiview frame