【问题标题】:Changing frame of view loaded from Xib更改从 Xib 加载的视图框架
【发布时间】: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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    把你的代码放进去

    - (void)viewDidLayoutSubviews
    {
    
    }
    

    它是一个委托方法,只有在自动布局执行后才会执行。

    【讨论】:

    • 这是在视图控制器还是视图中完成的?如您所见,我没有引用我正在添加的视图
    • 上面viewDidLoad里的代码应该放在这个方法里
    • 好像没有调用。
    • 好的,调用了,但是没有进展。
    • 我可以设置视图的中心,但是不能设置框架。
    猜你喜欢
    • 2016-09-30
    • 2022-01-02
    • 2023-03-16
    • 2019-10-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多