I am trying to change the frame of my custom view which I load from a xib.
Here is my init method of the view:
- (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;
}
This method is called from a view controller like this:
- (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.
}
Now, the line where it says view.frame = CGRectMake(0,100,320,100);
is where i try to change the views frame. I have tried putting that code inside the view's
- (id)initWithUserName:(NSString*)userName andComment:(NSString*)comment
method body as well , without luck.
So clearly I am having trouble resizing the view according to my preferences. The view has always the same frame as when it is presented in interface builder.
What am I doing wrong?
EDIT
I have tried putting the above viewDidLoad
body inside that viewcontroller's viewWillAppear
body, without any progress.
在.xib中禁用“使用Autolayout”解决了这个问题。
put your code inside
- (void)viewDidLayoutSubviews
{
}
it is a delegate method executes only after autolayout has executed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.