Programmatically created labels and images don't show on screen

烂漫一生 提交于 2019-12-12 04:19:38

问题


I have the following app, that's like a chat, so I'm programmatically creating some labels, images and buttons.
The problem is that the ones that I'm adding at startup do show and the ones that I'm adding afterwards don't.
It's like the view needs refreshing or something. If I call the same drawing function from an IBAction set to a button...it displays the content.. if it comes from the thread event that looks for new events and notifies the class through:

[[NSNotificationCenter defaultCenter] postNotificationName:@"NewMessageNotification" object:self userInfo:values];  

it then doesn't show anything. But the function is actually called (I've checked with the debugger) Could it be that it doesn't use the proper instance of the view?

here's my layout:

and here is my function that creates the controls:

    UILabel *labelMessage = [[UILabel alloc] initWithFrame:CGRectMake(left+5, lastcalculatedHeight-5, 220, calculatedHeight)];           
    [labelMessage setText:msg];
    [labelMessage setFont:[UIFont fontWithName:@"Arial" size:12.0]];
    [labelMessage setLineBreakMode:UILineBreakModeWordWrap];
    [labelMessage setTextColor:[UIColor blackColor]];
    [labelMessage setAdjustsFontSizeToFitWidth:NO];
    [labelMessage setNumberOfLines:floor( msg.length / 40 )+2];
    [labelMessage setBackgroundColor:[UIColor clearColor]];
    [scrollView addSubview:labelMessage];
    [labelMessage release];


    UIButton *buttonTime = [[UIButton alloc] initWithFrame:CGRectMake(left, lastcalculatedHeight+40, 50, 20)];  
    [buttonTime setBackgroundImage:[[UIImage imageNamed:@"hour_bubble.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:13] forState:UIControlStateDisabled];
    [buttonTime setFrame:CGRectMake(left_hour_button, lastcalculatedHeight+calculatedHeight-30, 55, 25)];
    [buttonTime setTitle:date2 forState:UIControlStateDisabled];                
    [buttonTime setTitleColor:[UIColor blackColor] forState:UIControlStateDisabled];
    buttonTime.titleLabel.font=[UIFont fontWithName:@"Helvetica" size:8.0]; 
    buttonTime.titleLabel.lineBreakMode= UILineBreakModeWordWrap;
    [buttonTime setEnabled:FALSE];
    [scrollView addSubview:buttonTime];
    [buttonTime release];

I also want to autoscroll the uiscrollview to bottom when I call this function..but it fails, it's like the simulator is going crazy when I call the following: I tried using

CGPoint offset = CGPointMake(0,lastcalculatedHeight);
[scrollView setContentOffset: offset animated: YES];

I got this from UIScrollView scroll to bottom programmatically


回答1:


The behavior you describe sounds like your notification is not being sent on the main thread; all UI updates must be done on the main thread. See my answer to your other question for some code to work around the problem.



来源:https://stackoverflow.com/questions/5409129/programmatically-created-labels-and-images-dont-show-on-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!