31

我已按照说明进行此处的说明成功设置了一个使用 UIDatePicker 进行更新的 UITextField。但是 UITextField 中的光标正在闪烁,这对我来说似乎有点尴尬。

有没有办法摆脱那个光标?

4

10 回答 10

48

我意识到这是一个老问题,但随着 iOS 7 的更新,现在可以通过执行以下操作来隐藏光标:

[[self textFieldName] setTintColor:[UIColor clearColor]];

但是,它仅适用于 iOS 7+。

于 2014-02-18T11:27:42.937 回答
40

子类 UITextfield 并覆盖该- (CGRect)caretRectForPosition:(UITextPosition *)position方法并返回CGRectZero

- (CGRect)caretRectForPosition:(UITextPosition *)position {
    return CGRectZero;
}
于 2013-03-21T09:06:20.693 回答
31

我希望它对你有帮助。

设置光标 UIColor -> 空。

 [[self.textField valueForKey:@"textInputTraits"] setValue:[UIColor clearColor] forKey:@"insertionPointColor"];

在斯威夫特:2.3

self.textField.valueForKey("textInputTraits")?.setValue(UIColor.clearColor() , forKey:"insertionPointColor")
于 2013-08-17T14:37:47.427 回答
6

我无法让 jcm 的解决方案发挥作用。我最终做的是继承 UILabel 以模仿 UITextField 的交互功能,而没有我不想要的部分(如光标)。我在这里写了一篇关于它的博客文章:

http://pietrorea.com/2012/07/how-to-hide-the-cursor-in-a-uitextfield/

基本上,UILabel 子类需要覆盖 isUserInteractionEnabled、inputView、inputViewAccessory 和 canBecomeFirstResponder。这只是几行代码,而且更有意义。

于 2012-07-06T20:57:21.863 回答
2

完全愚蠢的 hack,但是如果您在 Interface Builder 属性检查器的部分中设置文本字段的色调颜色UIView以匹配背景颜色,则光标将显示为不可见:

于 2016-01-29T20:07:28.447 回答
1

我所做的是将另一个 UITextField 覆盖在我想隐藏其光标的那个之上。然后在委托方法 textFieldShouldBeginEditing 中,我将另一个 textField 设置为第一响应者并返回 NO。

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag==TAG_OF_DUMMY_TEXTFIELD) {
        [otherField becomeFirstResponder];
        return NO;
    }
    return YES;
}

然后在日期选择器调用的方法中:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@YOUR_DATE_FORMAT];
dummyField.text = [dateFormatter stringFromDate:datePicker.date];

在 Interface Builder 中 otherField (带有 datePicker 输入视图的那个)在 dummyField (隐藏光标的那个)后面。

于 2011-09-29T02:37:10.653 回答
1

不是最好的解决方案,但您也可以将色调颜色的不透明度设置为 0%。

在此处输入图像描述

于 2016-11-15T13:38:57.317 回答
0

我发现这个解决方案是最容易实现的。

确保在 .h 文件中定义 UITextFieldDelegate:

.... UIViewController <UITextFieldDelegate>

在您的 .m 文件中,将此添加到您为日期选择器调用的方法中:

[yourTextField resignFirstResponder];

这将防止文本字段闪烁。

于 2013-01-01T18:05:10.383 回答
0

巴拉吉的方法确实有效。

我也多次使用过这样的 KVC 解决方案。尽管它似乎没有记录,但它确实有效。坦率地说,您在这里没有使用任何私有方法——只有合法的键值编码。

它与 [addNewCategoryTextField textInputTraits] 截然不同。

PS 昨天我的新应用出现在 AppStore 上,这种方法没有任何问题。当我使用 KVC 更改一些只读属性(如 navigatonBar)或私有 ivars 时,这不是第一种情况。

于 2013-09-16T18:59:58.267 回答
-1

您可以通过关联对象将BOOL cursorless属性添加到UITextField类别中。

@interface UITextField (Cursorless)

@property (nonatomic, assign) BOOL cursorless;

@end

然后使用方法 swizzlingcaretRectForPosition:与一个在CGRectZero其默认值之间切换的方法使用cursorless.

这通过插入类别导致了一个简单的界面。这在以下文件中得到了证明。

只需将它们放入并获得这个简单界面的好处

UITextField类别: https ://github.com/rexmas/RexDK/blob/master/RexDK/UI/UITextField%2BRXCursorless.h https://github.com/rexmas/RexDK/blob/master/RexDK/UI/UITextField%2BRXCursorless .m

方法混搭: https ://github.com/rexmas/RexDK/blob/master/RexDK/Foundation/NSObject%2BRXRuntimeAdditions.h https://github.com/rexmas/RexDK/blob/master/RexDK/Foundation/NSObject% 2BRXRuntimeAdditions.m

于 2014-05-19T02:15:53.333 回答