2011-12-19 85 views
1

我在UITableViewCellcontentView中有3個UI元素,需要在設備旋轉時正確對齊。目前,我執行這樣,這些UI元素的框架:UITableViewCell autoresizingmask問題

segmentedControl1.frame = CGRectMake(165, 15, 130, 40); 
segmentedControl2.frame = CGRectMake(435, 15, 130, 40); 
segmentedControl3.frame = CGRectMake(710, 15, 130, 40); 

我想知道我該如何使用這些元素的autoresizingMask屬性,使他們調整大小和彼此不重疊時,該設備從橫向旋轉到縱向(默認爲縱向,僅iPad)。我不想爲每個方向定製框架位置。

我想是這樣的:

segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin; 

但這似乎並沒有工作。這些元素都是重疊的,並且不幸地佈置。

有什麼建議嗎?

更新1

下面是表視圖單元格目前看起來像在橫向模式下:

-------------------------------------------------------------------------- 
| ============    ============    ============  | 
| | A | B |   | A | B |   | A | B | | 
| ============    ============    ============  | 
-------------------------------------------------------------------------- 

這裏就是我想要的,當設備被旋轉到肖像模式:

------------------------------------------------------- 
| ============ ============ ============  | 
| | A | B | | A | B | | A | B | | 
| ============ ============ ============  | 
------------------------------------------------------- 

UPDATE 2 這是我的代碼在合併的cellForRowAtIndexPath從建議@Wolfert

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    cell.contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth; 

    // Configure the cell... 
    segmentedControl1.frame = CGRectMake(165, 15, 180, 40); 
    segmentedControl1.tag = indexPath.row; 
    segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 
    [cell.contentView addSubview:segmentedControl1]; 

    segmentedControl2.frame = CGRectMake(435, 15, 180, 40); 
    segmentedControl2.tag = indexPath.row; 
    segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
    [cell.contentView addSubview:segmentedControl2]; 

    segmentedControl3.frame = CGRectMake(710, 15, 180, 40); 
    segmentedControl3.tag = indexPath.row; 
    segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
    [cell.contentView addSubview:segmentedControl3]; 

    return cell; 
} 

回答

3

這應該做的伎倆:

segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 
segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 

需要注意的是他們的超級觀點應該有這樣的屬性:

view.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
+1

這不起作用......對不起!還有什麼建議?我更新了一些圖形的問題,以顯示我在說什麼... – Ravi 2011-12-21 22:40:53

+0

對不起,我的錯。我複製了觀點來測試我的解決方案,但確實失敗了。 - 我已經更新了我的答案,並帶有工作和測試版本:) – Wolfert 2011-12-22 08:54:40

+0

嘿@Wolfert,我整合了你的建議,但它似乎仍然不起作用。事實上,通過上面的autoresizingMask值,segmentedControl2和segmentedControl3正在消失......我用我正在使用的實際代碼更新了我的問題。 – Ravi 2011-12-23 01:26:08

1

我有點困惑的是你說你有一個工作的景觀配置。我會說,結合你的硬編碼CGRectMake數字和你引用的autoresizingMask值已經導致了一個偏斜的佈局。

是的,因爲它可能,這裏是我的建議:你不應該使用硬編碼的數字爲您的初始幀的寬度。相反,你應該根據你的計算結果cell.contentView.bounds.size.width。在你的情況,這樣的事情:

- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    static NSString* cellIdentifier = @"Cell"; 
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease]; 

    NSArray* items = [NSArray arrayWithObjects:@"A", @"B", nil]; 
    UISegmentedControl* segmentedControl1 = [[[UISegmentedControl alloc] initWithItems:items] autorelease]; 
    UISegmentedControl* segmentedControl2 = [[[UISegmentedControl alloc] initWithItems:items] autorelease]; 
    UISegmentedControl* segmentedControl3 = [[[UISegmentedControl alloc] initWithItems:items] autorelease]; 
    [cell.contentView addSubview:segmentedControl1]; 
    [cell.contentView addSubview:segmentedControl2]; 
    [cell.contentView addSubview:segmentedControl3]; 

    // Some values I like 
    CGFloat marginHorizontal = 10; 
    CGFloat spacingHorizontal = 8; 
    // This is the crucial line! 
    CGFloat totalWidth = cell.contentView.bounds.size.width; 
    // That's how much is available to the three controls 
    CGFloat availableWidth = totalWidth - 2 * marginHorizontal - 2 * spacingHorizontal; 
    // That's how much each control gets - initially! 
    CGFloat segmentedControlWidth = availableWidth/3.0; 

    // These are the numbers from your example. With these numbers your table view 
    // delegate probably implements tableView:heightForRowAtIndexPath:() 
    CGFloat marginVertical = 15; 
    CGFloat segmentedControlHeight = 40; 

    CGRect segmentedControlFrame = CGRectMake(marginHorizontal, 
              marginVertical, 
              segmentedControlWidth, 
              segmentedControlHeight); 
    segmentedControl1.frame = segmentedControlFrame; 
    segmentedControlFrame.origin.x += spacingHorizontal + segmentedControlWidth; 
    segmentedControl2.frame = segmentedControlFrame; 
    segmentedControlFrame.origin.x += spacingHorizontal + segmentedControlWidth; 
    segmentedControl3.frame = segmentedControlFrame; 

    // These are the values you cited initially, they are fine 
    segmentedControl1.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 
    segmentedControl2.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 
    segmentedControl3.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin; 

    return cell; 
} 

我想這對iPad模擬器和它的工作就好了。請注意,您不必更改單元格或內容視圖的autoresizingMask。我知道,這是從UIView如何正常工作不同,但我沒有,除了表視圖細胞的解釋是奇獸:-)

回到你原來的代碼:問題的根源則存在硬編碼的數字假設內容視圖具有全屏寬度,實際上它的初始寬度要小得多(在我的環境中cell.contentView.bounds.size.width最初是320)。因此,最初的佈局看起來是這樣的:

+-content view------------+ 
| ============   | ============    ============ 
| | A | B |  | | A | B |   | A | B | 
| ============   | ============    ============ 
+-------------------------+ 

正如你所看到的,佈局是不相稱的內容視圖的初始寬度。當內容視圖後來調整到其正確的寬度時,控件的大小也會根據它們的初始寬度和水平分佈進行調整。這會導致更多的異議:-)