2011-08-23 54 views
0

我是iPad開發新手。在這裏,我有一個帶有動態內容的桌面視圖。每個單元格包含兩個標籤,第二個標籤可能增加或減少。最後,我將標籤添加到單元格。但是當我旋轉模擬器時,單元格中的標籤不會改變。我無法爲標籤設置框架。請幫幫我。如何在模擬器中旋轉iPad時設置標籤框架

在此先感謝。

UILabel *label1 = nil; 
UILabel *label2 = nil; 

NSLog(@"it decides the tablecell"); 
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease]; 

    label1 =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 44) ]; 
    //label1 = [[UILabel alloc] initWithFrame:CGRectZero]; 
    [label1 setLineBreakMode:UILineBreakModeWordWrap]; 
    [label1 setMinimumFontSize:FONT_SIZE]; 
    [label1 setNumberOfLines:0]; 
    [label1 setFont:[UIFont boldSystemFontOfSize:FONT_SIZE]]; 


    label2 = [[UILabel alloc] initWithFrame:CGRectZero]; 
    [label2 setMinimumFontSize:FONT_SIZE]; 
    [label2 setNumberOfLines:0]; 
    [label2 setLineBreakMode:UILineBreakModeWordWrap]; 
    [label2 setFont:[UIFont systemFontOfSize:FONT_SIZE]]; 


} 
NSString *text1 = [arrOutline1 objectAtIndex:[indexPath row]]; 
NSString *text2 = [arrOutline2 objectAtIndex:[indexPath row]]; 



CGSize constraint = CGSizeMake((CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2))/ 2, 20000.0f); 

CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 
CGSize size2 = [text2 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap]; 


[label1 setText:text1]; 
[label2 setText:text2]; 

    [label1 setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN-10,(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2))/2, MAX(size1.height, 44.0f))]; 

NSLog(@"now it is in cell methd"); 
UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
if (orientation == UIDeviceOrientationLandscapeLeft||orientation==UIDeviceOrientationLandscapeRight) 
{ 
    [label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,500 , MAX(size2.height, 44.0f))]; 
    NSLog(@"orientation is :LAndscape"); 
    //[[cell contentView] addSubview:label2]; 
    [cell addSubview:label2]; 

} 
else 
{ 
    NSLog(@"orientation is Portrait."); 

    [label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,200 , MAX(size2.height, 44.0f))]; 
    [cell addSubview:label2]; 


} 
    //[label2 setFrame:CGRectMake(300 , CELL_CONTENT_MARGIN - 10,(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2)) /2 , MAX(size2.height, 44.0f))]; 




cellHeight =size2.height + 44; 

label1.backgroundColor =[UIColor clearColor]; 
label2.backgroundColor =[UIColor clearColor]; 


[[cell contentView] addSubview:label1]; 
[label1 release]; 
[label2 release]; 

return cell; 

} 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

[TableOutline reloadData]; 
return YES; 
} 

回答

1

當iPad旋轉時,您需要自動調整子視圖的大小嗎?

1)請確保您的父視圖(INB你的情況我猜表視圖細胞)具有autoresizesSubviews設置爲YES(這將是默認)

2)請確保您添加標籤有正確的值設置爲autoresizingMask例如

// To keep label1 the same size and have label2 stretch to fill the rest of the cell . . . 
[label1 setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; 
[label2 setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 

在這裏,您將標籤1設置爲距離左側的固定距離並且是固定寬度。您已將標籤2設置爲距左側和右側的固定距離,但寬度可變。當表格單元格的框架發生變化時,它將自動更改其中的標籤框架。