2012-04-05 17 views
0

Wie würde ich über die dynamische Anpassung der Höhe eines UIScrollView gehen? Grundsätzlich habe ich eine Reihe von UILabels, die erstellt werden (die genaue Anzahl der UILabels ist zufällig) und die UIScrollView ist seine Höhe automatisch anpassen, um die UILables unterzubringen. Das habe ich bisher in der viewDidLoad.UIScrollView automatisch anpassen Höhe

- (void)viewDidLoad 
{ 
[scroller setScrollEnabled:YES]; 
[scroller setContentSize:CGSizeMake(320 
            , 1500)]; 
scroller.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
{ 

Und dies ist die Aktion, die

-(IBAction)scheduale{ 
int i; 
for(i=0; i<[self retrieveTime] ; i++){ 
    //Add time label 
    UILabel *timeLabel = [[UILabel alloc] init]; 
    timeLabel.frame = CGRectMake(10, (i+1) * 21, 31, 20); 
    timeLabel.textColor = [UIColor whiteColor]; 
    timeLabel.backgroundColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0]; 
    NSString *labelString; 
    labelString = [[NSNumber numberWithInt:i] stringValue]; 
    timeLabel.text = labelString; 
    timeLabel.textAlignment = UITextAlignmentCenter; 
    //theLabel.tag = (i+1) * 100; 
    [scroller addSubview:timeLabel]; 
} 

Antwort

2

Sie benötigen zusätzliche UILavels schafft auf eine Variable zu halten, um zu verfolgen, wie viele Etiketten Sie hinzufügen und deren Höhe die Inhaltsgröße einzustellen, wenn Sie sind getan, Etiketten zu erstellen. Siehe unten angepassten Code:

-(IBAction)scheduale{ 

int i; 
int contentSize = 0; 

for(i=0; i<[self retrieveTime] ; i++){ 

    UILabel *timeLabel = [[UILabel alloc] init]; 
    timeLabel.frame = CGRectMake(10, (i+1) * 21, 31, 20); 
    contentSize += 20; 
    timeLabel.textColor = [UIColor whiteColor]; 
    timeLabel.backgroundColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0]; 
    NSString *labelString; 
    labelString = [[NSNumber numberWithInt:i] stringValue]; 
    timeLabel.text = labelString; 
    timeLabel.textAlignment = UITextAlignmentCenter; 
    //theLabel.tag = (i+1) * 100; 
    [scroller addSubview:timeLabel]; } 

[scroller setContentSize:CGSizeMake(320, contentSize)]; 
+0

Bitte überprüfen Sie, dass Content richtig geht. – danh

0

ich mit @ Kyle zustimmen, aber der Inhalt Größe wird mit den Positionen der Etiketten vorantreiben, wobei die letzten Position endend + letzter Höhe. Also muss seine Idee ein wenig zwicken.

Auch, nur um zu bestätigen: @world peace - möchten Sie den Rahmen des Scrollers ändern, oder nur die Inhaltsgröße? @Kyle ist korrekt, dass Sie mindestens die Inhaltsgröße ändern müssen.

Hier ist eine kleine Bereinigung auf diesem Code:

#define kButtonWidth 31.0 
#define kButtonHeight 20.0 
#define kMargin   1.0 

-(IBAction)scheduale { 

    int i; 
    CGFloat contentPositionY = 0.0; 

    UIColor *labelColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0]; 

    for(i=0; i<[self retrieveTime] ; i++){ 
     //Add time label 
     UILabel *timeLabel = [[UILabel alloc] init]; 

     timeLabel.frame = CGRectMake(0, contentPositionY, kButtonWidth, kButtonHeight); 
     contentPositionY += kButtonHeight + kMargin; 

     timeLabel.textColor = [UIColor whiteColor]; 
     timeLabel.backgroundColor = labelColor; 

     timeLabel.text = [NSString stringWithFormat:@"@d", i]; 

     timeLabel.textAlignment = UITextAlignmentCenter; 
     //theLabel.tag = (i+1) * 100; 
     [scroller addSubview:timeLabel]; 
    } 

    // now size the content 
    scroller.contentSize = CGSizeMake(kButtonWidth, contentPositionY + kButtonHeight); 

    // and to get the margins you built into the loop calculation 
    scroller.contentInset = UIEdgeInsetsMake(21, 10, 0, 0); // your code implied these values 
} 
Verwandte Themen