【问题标题】:setFrame of UILabel property of .xib doesn´t work.xib 的 UILabel 属性的 setFrame 不起作用
【发布时间】:2012-09-12 11:45:39
【问题描述】:

我在 viewController 中创建下一个方法

-(void)setupDocsLabel:(NSMutableArray *)documents{      
    self.lDocumentos.frame = CGRectMake(self.lDocumentos.frame.origin.x, kFirstLabelYPosition+actualLabelYPos,self.lDocumentos.frame.size.width,lDocumentos.frame.size.height);
    self.Documentos.frame = CGRectMake(self.Documentos.frame.origin.x, kFirstLabelYPosition+actualLabelYPos,self.Documentos.frame.size.width,self.Documentos.frame.size.height);

    actualLabelYPos +=20.0;


    for (DocInformation *doc in documents) {
        NSString *textLabel = [doc.documentDescription stringByAppendingString:@" :"];
        UIFont *lblFont = lDocumentos.font;
        CGSize sizeFont = [textLabel sizeWithFont:lblFont forWidth:120.0 lineBreakMode:NSLineBreakByTruncatingTail];

        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(lDocumentos.frame.origin.x+20, kFirstLabelYPosition+actualLabelYPos,sizeFont.width,sizeFont.height)] retain];
        label.text = textLabel;
        [label setFont:lblFont];
        [label setTextColor:lDocumentos.textColor];
        [label setBackgroundColor:[UIColor clearColor]];
        //[label setLineBreakMode:NSLineBreakByTruncatingTail];


        NSString *textDoc = doc.cdgoDocum;
        UIFont *lblFontDoc = Documentos.font;
        CGSize sizeFontDoc = [textDoc sizeWithFont:lblFontDoc];

        UILabel *labelDoc = [[[UILabel alloc] initWithFrame:CGRectMake(label.frame.origin.x+label.frame.size.width+20, kFirstLabelYPosition+actualLabelYPos,sizeFontDoc.width,lDocumentos.frame.size.height)] retain];
        labelDoc.text = textDoc;
        [labelDoc setFont:lblFontDoc];
        [labelDoc setTextColor:lDocumentos.textColor];
        [labelDoc setBackgroundColor:[UIColor clearColor]];

        [self.scrollView addSubview:label];
        [self.scrollView addSubview:labelDoc];

        [label release];
        [labelDoc release];

        actualLabelYPos+=20.0;
    }
    //[self.view setNeedsDisplay];
    //[self.view setNeedsLayout];
}

新标签很好地添加到滚动视图,但 self.lDocumentos(.xib 的 IBOutlet)不会改变他的位置。

感谢您的帮助!

【问题讨论】:

  • 什么时候调用setupDocsLabel方法?请记住,IBOutlets 是在 viewDidLoad 上分配的,当您访问 view 属性时会延迟加载
  • 方法在viewDidLoad中被调用。当我检查该属性时,它被实例化了。我不知道我还能尝试什么。

标签: ios uiscrollview uilabel frame


【解决方案1】:

我已经解决了,问题是 .xib 在 File Inspector 中启用了 Use AutoLayout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    相关资源
    最近更新 更多