0

I downloaded the Collection View example and I replaced the ImageView with textfield. I want to make the textfield as big as the UICollectionViewCell (which is a square shape). But the shape of the textfield didn't change.

- (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

code4j
  • 4,208
  • 5
  • 34
  • 51

4 Answers4

0

You can do that by changing the border style of UITextField.

See this Stackoverflow post: How to set border style of a UITextfield

Community
  • 1
  • 1
Akhil PK
  • 91
  • 5
0

Do you have set the Tag of your UITextfield to 100 in XCode Designer? You have to set the Tag to 100 before calling your code, to ensure the UITextfield to be found.

Changing Border Style is not necessary, because you can do it with every Borderstyle.... I have done it already...

Go to your cellItem Nib in XCode Designer, select the UITextField and in the right properties, change the tag to 100... This will solve it

0

I have found the root cause of this problem. The root cause is the auto layout, it resizes the width, height and the space between the cell and the textfield at the runtime. Once I removed the auto layout, the code work fine.... And the border style is not a matter.

I am looking for another way to solve this problem.

code4j
  • 4,208
  • 5
  • 34
  • 51
0

just try this..

NSInteger myCustomHeight = 237;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, myCustomHeight)];
Rahul
  • 134
  • 6