我正在尝试更改从xib加载的自定义视图的框架。
以下是我的视图初始化方法:
- (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
主体放在该viewcontroller的viewWillAppear
主体中,没有任何进展。
答案 0 :(得分:5)
在.xib中禁用“使用Autolayout”解决了这个问题。
答案 1 :(得分:3)
将您的代码放入
- (void)viewDidLayoutSubviews
{
}
只有在执行autolayout后才会执行委托方法。