我正在尝试更改从 xib 加载的自定义视图的框架。
这是我的视图的init方法:
- (id)initWithUserName:(NSString*)userName andComment:(NSString*)comment
{
if (self) {
self = [[[NSBundle mainBundle] loadNibNamed:@"Comment" owner:self options:nil]objectAtIndex:0];
self.userName = userName;
self.comment = comment;
NSLog(@"CREATED");
NSLog(@"%@, %@",self.userName, self.comment);
self.userNameButton.tag = -1;
CGSize constraint = CGSizeMake(240, 2000);
self.userNameButton.backgroundColor = [UIColor clearColor];
CGSize sizeOfUsernameText = [self.userName sizeWithFont:[UIFont fontWithName:@"OpenSans-Light" size:12.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
//color converted from #161616 Hex
[self.userNameButton.titleLabel setTextColor:[StaticMethods colorFromHexString:@"#161616"]];
[self.userNameButton setTitle:[self.userName stringByAppendingString:@":"] forState:UIControlStateNormal];
[self.userNameButton.titleLabel setFont:[UIFont fontWithName:@"OpenSans-Light" size:12.0]];
[self.userNameButton setNeedsDisplay];
//[self.userNameButton addTarget:self.delegate action:@selector(userNameButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
//Append appropriate amount so that the usernames can be seen and clickable
NSString *indentString = @" ";
CGSize sizeOfWhitespaces = [indentString sizeWithFont:[UIFont fontWithName:@"OpenSans-Light" size:12.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
while (sizeOfWhitespaces.width < sizeOfUsernameText.width) {
sizeOfWhitespaces = [indentString sizeWithFont:[UIFont fontWithName:@"OpenSans-Light" size:12.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
indentString = [indentString stringByAppendingString: @" "];
}
CGSize sizeOfComment = [comment sizeWithFont:[UIFont fontWithName:@"OpenSans-Light" size:12.0] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
self.commentText.tag = -1;
self.commentText.text = [indentString stringByAppendingString: self.comment];
self.commentText.font = [UIFont fontWithName:@"OpenSans-Light" size:12.0];
[self.commentText setTextColor:[StaticMethods colorFromHexString:@"#161616"]];
}
return self;
}
从视图控制器调用此方法,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
for(int i = 0; i < self.userNames.count;i++){
CommentView *view = [[CommentView alloc]initWithUserName:[self.userNames objectAtIndex:i] andComment:[self.comments objectAtIndex:i]];
[view setDelegate: self];
[self.view addSubview:view];
view.frame = CGRectMake(0,100,320,100);
}
// Do any additional setup after loading the view from its nib.
}
现在,它说的那一行view.frame = CGRectMake(0,100,320,100);
是我尝试更改视图框架的地方。我已经尝试将该代码放在视图中
- (id)initWithUserName:(NSString*)userName andComment:(NSString*)comment
方法体也是如此,没有运气。
很明显,我无法根据自己的喜好调整视图大小。视图始终具有与在界面构建器中呈现时相同的框架。
我究竟做错了什么?
编辑
我尝试将上述viewDidLoad
主体放入该视图控制器的viewWillAppear
主体中,但没有任何进展。