开发者

UIView or UIScrollView doesn't refresh the display on programmatically created controls

开发者 https://www.devze.com 2023-02-19 19:47 出处:网络
I\'m creating melodramatically some controls, and they don\'t appear, it needs some kind of a refresh method..

I'm creating melodramatically some controls, and they don't appear, it needs some kind of a refresh method.. here's my layout:

UIView or UIScrollView doesn't refresh the display on programmatically created controls

it's a chat, and all those messages I'm creating them dynamically as the event appears, what I'm creating at startup, it does appear, but what I'm creating afterward fails to show, even if the code is ok,

here's how am I creating the controls (by calling the following function):

- (void) AddNewMessage: (NSString *)msg date1:(NSString* ) date2  {

    NSString *backimage =[NSString stringWithString:@""];
    NSString *who =[NSString stringWithString:@"1"];

    calculatedHeight=floor( msg.length / 40 )*20;
        if (calculatedHeight<30) calculatedHeight=30;
    if ([who isEqualToString:@"1"]) { 
        left=85;
        left_hour_button=25;
        backimage=@"green_bubble.png";
    }
    else {
        left=5;
        left_hour_button=240;
        backimage=@"grey_bubble.png";
    }

    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(left, lastcalculatedHeight-10, 230, calculatedHeight+10)];   
    [button1 setBackgroundImage:[[UIImage imageNamed:backimage] stretchableImageWithLeftCapWidth:9 topCapHeight:13] forState:UIControlStateDisabled];
    [button1 setEnabled:FALSE];          
    [scrollView addSubview:button1];
    [button1 release];

    UIButton *buttonTime = [[UIButton alloc] initWithFrame:CGRectMake(left, lastcalculatedHeight+40, 50, 20)];  
    [buttonTime setBackgroundImage:[[UIImage imageNamed:@"hour_bubble.png"] stretchableImageWithLeftCa开发者_JAVA技巧pWidth: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];


    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];

    lastcalculatedHeight+=calculatedHeight+20;

    totalHeight+=calculatedHeight;  

    scrollView.contentSize = CGSizeMake(240, lastcalculatedHeight);
}

How do I "refresh" the display?

The scrollView is an object inside the Uiview from which the class derives

Also I will want to scroll down to the bottom the scrollview, each time I'm adding a new message.

I've seen this post but either it doesn't work, or it acts weird on the simulator... haven't tried it on a device because I don't own an iPhone yet.

it scrolls too much or at all, but if I go to another tab, then switch back, the scroll is scrolled at the bottom, and it even shows the controls I've created programmatically... Weird isn't it?


Try using [self.view setNeedsDisplay], if self is a view controller, or just [scrollView setNeedsDisplay].

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号