2013-03-15 162 views
0

我下載了Collection View example,我用文本字段替換了ImageView。 我想使文本字段與UICollectionViewCell一樣大(這是一個正方形形狀)。但是textfield的形狀沒有改變。如何製作一個與UICollectionViewCell一樣大的文本字段?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    static NSString *identifier = @"Cell"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    // height and width in the parent cell 
    double height = cell.frame.size.height; 
    double width = cell.frame.size.width; 

    // get the child textfield 
    UITextField *textField = (UITextField *)[cell viewWithTag:100]; 

    // make the textfield as big as the cell 
    CGRect frame = CGRectMake(0, 0, width, height); 
    textField.frame =frame; 
    textField.text = @"testing"; 

    return cell; 
} 

storyboard and simulator

回答

0

你已經確立了自己的UITextField 100 XCode中設計的標籤?在調用代碼之前,必須將標記設置爲100,以確保找到UITextfield。

更改邊框樣式是沒有必要的,因爲你可以與每一個邊框做到這一點....我已經做了,它已經...

轉到您的cellItem筆尖在XCode的設計,選擇的UITextField,並在正確的屬性,將標籤更改爲100 ...這將解決它

0

我找到了此問題的根本原因。根本原因是自動佈局,它在運行時調整單元格和文本字段之間的寬度,高度和空間大小。一旦我刪除了自動佈局,代碼工作正常....而邊框樣式不是問題。

我正在尋找另一種方法來解決這個問題。

+0

嘗試使用一個TextView來代替。 – AMayes 2013-03-15 17:00:51

0

只是嘗試這個..

NSInteger myCustomHeight = 237; 
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, myCustomHeight)]; 
+0

你的意思是在運行時添加textField嗎? 我試過但它不起作用。 – code4j 2013-03-15 16:36:20

+0

你在哪裏定義了文本字段。在那個循環中。 – Rahul 2013-03-18 05:35:04

相關問題