ios - UITextFields 不允许我编辑文本

标签 ios objective-c uitextfield

我在我的 iOS 应用程序中以编程方式创建了两个 UITextField,并将它们的文本分别设置为 _minPrice_minPrice 变量。

_minPrice_maxPrice 值在这两个字段中正确显示,但是点击它们不允许用户编辑它们,它们只是保留那些静态值,退格不起作用。我的代码是否有任何内容阻止编辑文本字段?

    // Min Price
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(-25, -76, 70, 30)];
    tf.textColor = [UIColor blackColor];
    tf.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf.backgroundColor=[UIColor whiteColor];
    tf.text= _minPrice;

    tf.textAlignment = NSTextAlignmentCenter;
    tf.layer.cornerRadius=8.0f;
    tf.layer.masksToBounds=YES;
    tf.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf.layer.borderWidth= 1.0f;

    // Max Price
    UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, -76, 70, 30)];
    tf1.textColor = [UIColor blackColor];
    tf1.font = [UIFont fontWithName:@"Helvetica-Neue" size:14];
    tf1.backgroundColor=[UIColor whiteColor];
    tf1.text= _maxPrice;

    tf1.textAlignment = NSTextAlignmentCenter;
    tf1.layer.cornerRadius=8.0f;
    tf1.layer.masksToBounds=YES;
    tf1.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    tf1.layer.borderWidth= 1.0f;

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 200, 400, 400)];
    [view addSubview:tf];
    [view addSubview:tf1];

    [self.view addSubview:view];

最佳答案

您的问题显然是您设置的框架...

将添加标签的 View 的颜色设置为蓝色可以揭示您的问题:

enter image description here

如果您确保标签确实在您添加它们的 View 中(即不是负数),编辑就没问题。我所做的只是将 tf 中的负 x 和 y 值更改为正值,并且它们是可编辑的:

UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(25, 76, 70, 30)];

enter image description here

关于ios - UITextFields 不允许我编辑文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26047989/

相关文章:

objective-c - 如何在 Cocoa 应用程序中将 NSView 添加到 NSWindow?

ios - UITextField——观察 selectedTextRange 的变化?

objective-c - 如何使 UIPopoverController 在旋转后保持相同的位置?

ios - 在 iPad 中处理不同方向的最佳方式

ios - 使用 block 枚举/迭代数组并知道它何时完成

ios - RestKit 文本/html 序列化问题

objective-c - 如何用触摸手势控制 CAKeyframeAnimation?

编辑时iOS UITextField未清除

objective-c - objective-c 中的UITextField shouldChangeCharactersInRange方法

ios - 目标debug_ios_bundle_flutter_assets失败: