【问题标题】:How to make a textfield as big as a UICollectionViewCell?如何使文本字段与 UICollectionViewCell 一样大?
【发布时间】:2013-03-15 09:33:46
【问题描述】:

我下载了Collection View example,并用文本字段替换了 ImageView。 我想让文本字段和 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;
}

【问题讨论】:

    标签: ios objective-c ios6 uitextfield uicollectionview


    【解决方案1】:

    您是否在 XCode Designer 中将 UITextfield 的 Tag 设置为 100?您必须在调用代码之前将 Tag 设置为 100,以确保找到 UITextfield。

    没有必要更改边框样式,因为您可以对每个边框样式进行更改...我已经这样做了...

    在 XCode Designer 中转到您的 cellItem Nib,选择 UITextField 并在正确的属性中,将标签更改为 100... 这将解决它

    【讨论】:

      【解决方案2】:

      您可以通过更改 UITextField 的边框样式来做到这一点。

      查看这篇 Stackoverflow 帖子:How to set border style of a UITextfield

      【讨论】:

        【解决方案3】:

        我找到了此问题的根本原因。根本原因是自动布局,它会在运行时调整单元格和文本字段之间的宽度、高度和空间。一旦我删除了自动布局,代码就可以正常工作了......而且边框样式不是问题。

        我正在寻找另一种方法来解决这个问题。

        【讨论】:

        • 尝试使用文本视图。
        【解决方案4】:

        试试这个..

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

        【讨论】:

        • 您定义文本字段的位置。在那个循环中。
        猜你喜欢
        • 2022-07-26
        • 1970-01-01
        • 2020-09-20
        • 1970-01-01
        • 1970-01-01
        • 2019-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多