2012-01-16 61 views
0

我在桌面視圖中製作了一個iPad應用程序,其中名爲crollTableView的tableView我在每行的單元格中創建兩個標籤,其名稱爲capitalLabel和stateLabel在人像模式,無法在橫向模式下增加UILabel的寬度

現在我想提高我在橫向模式下標籤的寬度,

這裏是代碼片段,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

if(tableView==crollTableView){ 
     static NSString *[email protected]"Cell"; 
     static NSInteger StateTag = 1; 
     static NSInteger CapitalTag = 2; 
     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if(cell == nil){ 

      // cell.backgroundColor=[UIColor yellowColor]; 
      cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease]; 
      CGRect frame; 
      frame.origin.x = 0; 
      frame.origin.y = 0; 
      frame.size.height = 70; 
      frame.size.width = 650; 


      UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame]; 
      capitalLabel.tag = CapitalTag; 
      capitalLabel.opaque = FALSE; 
      capitalLabel.font=[UIFont systemFontOfSize:20.0]; 
      [cell.contentView addSubview:capitalLabel]; 


      frame.origin.x += 680; 
      UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame]; 
      stateLabel.tag = StateTag; 
      stateLabel.font=[UIFont systemFontOfSize:17.0]; 
      [cell.contentView addSubview:stateLabel]; 
     } 

     UILabel* capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag]; 
     UILabel* stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag]; 

     capitalLabel.text=[news2 objectAtIndex:indexPath.row]; 
     stateLabel.text = [news3 objectAtIndex:indexPath.row]; 

     capitalLabel.backgroundColor = [UIColor clearColor]; 
     stateLabel.backgroundColor = [UIColor clearColor]; 

     capitalLabel.textColor=[UIColor whiteColor]; 
     stateLabel.textColor=[UIColor whiteColor]; 

     if(count<[news3 count]){ 
      capitalLabel.numberOfLines = 0; 
      [capitalLabel sizeToFit]; 

      stateLabel.numberOfLines = 0; 
      [stateLabel sizeToFit]; 

     }count++; 

     return cell; 
    } 
} 

我應該在我的代碼做什麼樣的變化?

+0

你必須檢查第一取向之後,你可以根據自己的定位來設置標籤的大小。 – Hiren 2012-01-16 06:55:25

回答

1

您應該重寫

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations. 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

通過檢查條件的電流方向是橫向還是縱向,您可以相應地設置標籤的框架。

+0

它工作!!!!!! – GAMA 2012-01-19 10:29:15

1

嘗試設置autoresizingMask到要自動縮放視圖:

if (cell == nil)標籤alloc+init後添加:

capitalLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 

stateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
1

iOS中不關心增加寬度的任何控制時變化方向..只需將2個標籤的autoresizing mask屬性設置爲UIViewAutoresizingFlexibleWidth ..這意味着您的控件的寬度將根據超級視圖框的大小而改變。

0

嘗試設置autoResizingMask,以便在方向更改期間視圖會自動調整自身。

您可以更改Xcode尺寸檢查器中的值。

enter image description here