2014-10-29 83 views
0

我正在嘗試將此tableview部分標題視圖調整爲自動佈局,因此它將適用於iPhone 6和6 Plus。這是所期望的結果 desired result右對齊視圖不工作的自動佈局

具有命名stringViewfavoriteCountheartImage 3次。以下屏幕截圖將在每個視圖上設置灰色背景,以便更容易地看到發生了什麼。

我不明白的是,以下

@"|-(15.0)-[stringView]-(>=20.0)-[favoriteCount(44.0)]-[heartImage(22.0)]-(10.0)-|" 

產生 example 1

與計數和心臟意見不見蹤影。所以感覺好像包含的視圖比它所需要的要寬得多,但我不知道我有任何控制權,因爲從這個方法返回的視圖應該自動按照tableview的寬度調整大小,不應該這樣做?

BTW這個tableView:viewForHeaderInSection:方法,它看起來像

- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section { 

    if (section == 0) { 
     // My Favourites menu 
     UIView *myFavoritesMenuView = [[UIView alloc] init]; 
     [myFavoritesMenuView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:1.0]]; 

     UILabel *stringView = [[UILabel alloc] init]; 
     [stringView setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     [stringView setText:@"My Favourites"]; 
     [stringView setTextColor:[UIColor whiteColor]]; 
     [stringView setFont:[UIFont fontWithName:@"OpenSans" size:16.0]]; 
     [stringView setBackgroundColor:[UIColor grayColor]]; 
     [myFavoritesMenuView addSubview:stringView]; 

     NSDictionary *favoriteArticles = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"FavoriteArticles"]; 
     UILabel *favoriteCount = [[UILabel alloc] init]; 
     [favoriteCount setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     [favoriteCount setFont:[UIFont fontWithName:@"OpenSans" size:16.0]]; 
     [favoriteCount setTextColor:[UIColor whiteColor]]; 
     [favoriteCount setTextAlignment:NSTextAlignmentRight]; 
     [favoriteCount setBackgroundColor:[UIColor grayColor]]; 
     [myFavoritesMenuView addSubview:favoriteCount]; 

     if (favoriteArticles && favoriteArticles.count > 0) { 
      [favoriteCount setText:[NSString stringWithFormat:@"%lu", (unsigned long)favoriteArticles.count]]; 
     } else { 
      [favoriteCount setText:@""]; 
     } 

     UIImageView *heartImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"heart-button-on"]]; 
     [heartImage setTranslatesAutoresizingMaskIntoConstraints:NO]; 
     [heartImage setBackgroundColor:[UIColor grayColor]]; 
     [myFavoritesMenuView addSubview:heartImage]; 

     // Create the views dictionary 
     NSDictionary *views = NSDictionaryOfVariableBindings(stringView, favoriteCount, heartImage); 

     // Horizontal layout - note the options for aligning the vertical center of all views 
     NSString *horizontalFormat = @"|-(15.0)-[stringView]-(>=20.0)-[favoriteCount(44.0)]-[heartImage(22.0)]-(10.0)-|"; 

     [myFavoritesMenuView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:horizontalFormat 
                        options:NSLayoutFormatAlignAllCenterY 
                        metrics:nil 
                         views:views]]; 

     // Vertical layout - we only need one "column" of information because of the alignment options used when creating the horizontal layout 
     NSString *verticalFormat = @"V:|-[stringView]-|"; 
     [myFavoritesMenuView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:verticalFormat 
                        options:0 
                        metrics:nil 
                         views:views]]; 

     return myFavoritesMenuView; 
    } 

    // handle other section headers here 
} 

順便內部所有發生的事情,我也通過創建一個UIView作爲單獨的廈門國際銀行試過,只包含文本標籤左對齊和心臟圖像對齊到超視圖的後緣,並且看到與心臟完全相同的問題不可見(可能是在屏幕右側)。

+0

是否在代碼中創建了標題視圖?確保你調用[myHeaderView setTranslatesAutoresizingMasksIntoConstraints:NO]。 – 2014-10-29 14:03:26

+0

顯示你在tableView:viewForHeaderInSection中的完整代碼:所以我們可以看到你是如何創建這個視圖的。您顯示的限制條件應該可以正常工作(假設您也有正確的垂直限制)。 – rdelmar 2014-10-29 23:32:23

+0

@MikeTaverne是在代碼中創建標題視圖。如果我調用[myFavoritesMenuView setTranslatesAutoresizingMaskIntoConstraints:NO];那麼這個容器視圖甚至不會出現在表格中。 – martinjbaker 2014-10-30 06:13:52

回答

0

整個表格視圖的寬度不正確(由於某種原因,表格視圖是方形的,而不是適合屏幕),所以缺少的子視圖在屏幕右側。當我在橫向視圖中更改爲在iPhone 6中運行並且子視圖出現時發現它...

+0

您是否設置了表視圖的約束條件?如果是的話,你可以告訴我們他們嗎? – valfer 2014-11-01 07:22:09

+0

事實證明,我沒有在故事板的表格視圖中手動設置約束,它使用的是它設置的任何大小。由於表格視圖在更改觀看寬度和高度時在故事板編輯器中調整大小,因此不會將明智約束添加(將其與超視圖大小相匹配),而不是將其設置爲當前大小。 – martinjbaker 2014-11-02 12:00:09

+0

1)什麼意思是「改變觀看寬度和高度」:哪個觀看? 2)在故事板設置中選中或取消選中「使用大小類別」和「使用自動佈局」? – valfer 2014-11-03 07:55:13