【发布时间】:2013-08-09 18:51:47
【问题描述】:
我正在尝试更改从 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 正文放入该视图控制器的 viewWillAppear 正文中,但没有任何进展。
【问题讨论】:
-
是否启用了自动布局?
-
是启用自动布局
-
你必须声明 -(id)initWithFrame:(CGRect)frame;名为 commentView 的自定义子类中的 meyhod
-
即使视图是从笔尖加载的?我需要那个方法来设置框架吗?
-
您的 initWithMethod 表明该视图是以编程方式创建的。您是如何创建它的
标签: ios cocoa-touch interface-builder xib