gpt4 book ai didi

ios - 更改从 Xib 加载的 View 框架

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:02:55 26 4
gpt4 key购买 nike

我正在尝试更改从 xib 加载的自定义 View 的框架。

这是我的 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;
}

此方法从 View Controller 调用,如下所示:

- (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); 的行是我尝试更改 View 框架的地方。 我已经尝试将该代码放入 View 中

- (id)initWithUserName:(NSString*)userName andComment:(NSString*)comment

还有方法体,没有运气。

很明显,我无法根据自己的喜好调整 View 大小。 View 始终具有与在界面构建器中呈现时相同的框架。

我做错了什么?

编辑

我已经尝试将上面的 viewDidLoad 主体放入该 View Controller 的 viewWillAppear 主体中,但没有任何进展。

最佳答案

在 .xib 中禁用“使用自动布局”为我解决了这个问题。

关于ios - 更改从 Xib 加载的 View 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18119588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com