2014-09-10 74 views
1

我在構建應用程序時第一次學習iOS。在要求之一,UITableViewCell需要看起來像
enter image description here (PS:這只是一個設計,沒有代碼爲這個寫的)iOS CustomTableViewCell:元素沒有正確對齊

然而,當我設計我UITableViewCell,它看起來像

enter image description here
enter image description here enter image description here enter image description here

和代碼生成這個樣子

- (void)setTransactionCell:(TransactionCell *)transactionCell transactionModel:(TransactionModel *)transactionModel { 
    transactionCell.dateOfMonth.image = [self getDayImage:[UIImage imageNamed:@"1"]]; 
    transactionCell.dayOfWeek.image = [self getDayImage:[UIImage imageNamed:@"Sun"]]; 

    transactionCell.name.text = transactionModel.name; 
    transactionCell.name.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15]; 

    transactionCell.amount.text = transactionModel.amount; 
    transactionCell.amount.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15]; 

    transactionCell.categoryImage.image = [self getCategoryImage:[UIImage imageNamed:transactionModel.category]]; 
    transactionCell.imageView.contentMode = UIViewContentModeCenter; 
} 

- (UIImage *)getDayImage: (UIImage *) image { 
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(30, 30), NO, 0); 
    [image drawInRect:CGRectMake(0, 0, 15, 15)]; 
    UIImage *im2 = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return im2; 
} 

- (UIImage *)getCategoryImage:(UIImage *)image { 
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(40, 40), NO, 0); 
    [image drawInRect:CGRectMake(0, 0, 36, 36)]; 
    UIImage *im2 = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return im2; 
} 

最後當我運行應用程序,我看到
enter image description here

我是相當新的,不知道是怎麼了。我拼命嘗試拖拽標籤和圖像到每個可能的組合,但沒有任何工作。我錯過了一些非常基本的東西,你能指導一下什麼是錯的嗎?

回答

1

xibs上的對象每個對象至少需要4個約束。當標籤/ UIImageView對象周圍出現橙色線條時,這意味着約束丟失或衝突,並且通常意味着圖像在運行時不會像您期望的那樣顯示。

對約束進行排序的最簡單方法是使用界面生成器。首先,我建議選擇每個對象並通過從IB底部的圖標行中選擇選項來清除當前約束(請參閱下面的圖片)

enter image description here

一旦這個已被清除,增加新的約束再次選擇對象,然後選擇左側的圖標,你用來清除限制(跨一個一個 - 見挑)

在這幅圖中,您會看到一個彈出窗口,讓您可以選擇添加約束,在這種情況下,我選擇了UILabel的頂部和左側的空間約束到他們最近的頂部和左側的對象。您可以通過爲特定約束選擇彈簧來實現此目的,並且這會變成橙色以顯示它已被選中。 (這滿足了所需的最小4個約束中的兩個)。接下來的兩個約束(因爲我不希望UIlabel在那裏調整大小)是約束高度和寬度。 (放入框中打勾選擇每個)

enter image description here

這必須是可重複的,你有你的廈門國際銀行每一個UI對象。一旦有適當的約束,該線將全部是藍色的,這意味着沒有警告(見PIC下面 - 的UIImageView的約束高亮藍)

enter image description here

我希望這可以幫助你,但我建議你詳細瞭解自動佈局約束。他們可以是非常有用的,當你得到他們的hang and,甚至可以是動畫等等等等

+0

非常感謝吉姆,你的回答幫助我非常整齊地理解這一點,我甚至沒有看到任何其他的自動佈局,並能設計http://imgur.com/aJiPuTt。再次感謝 – daydreamer 2014-09-11 16:47:21