首页 > 学院 > 开发设计 > 正文

[iOS]UITextView放在UITableView中选中不显示光标问题

2019-11-14 18:19:38
字体:
来源:转载
供稿:网友

原文地址请保留http://www.VEVb.com/rossoneri/p/4809692.html
很奇怪的问题,一个UITextView放进UITableView中作为一个Cell,首先设置选中高亮:

[self setSelectionStyle:UITableViewCellSelectionStyleBlue];

这里说一点,在ipAD中,UITableView的选中高亮好像永远是灰色的。
然后点击UITextView进入编辑状态时,键盘会弹出,也可以输入内容,然而不显示光标。
好了,有人说背景色和光标一致导致看不见,然而我改了光标颜色以及背景色还是没什么卵用。
好了,又有人说把选中高亮去掉就行了,然而我若不用高亮我干嘛还要研究这个问题。

折腾了好久,看到一个这样的答案link

Cell stopped to hide cursor, when i set.

cell.selectionStyle = UITableViewCellSelectionStyleNone;

But after that, cell also stopped use selectedBackgroundView. So I set my image to highlighted state of backgroundView.

cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"input_up.png"] highlightedImage:[UIImage imageNamed:@"input_up_act.png"]] autorelease];

And switched image in setSelected:animated:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {    [super setSelected:selected animated:animated];    if (selected && ![textView isFirstResponder]) {        [textView setUserInteractionEnabled:YES];        [textView becomeFirstResponder];    } else {        [textView resignFirstResponder];        [textView setUserInteractionEnabled:NO];    }    [(UIImageView *)self.backgroundView setHighlighted:selected];}

这个思路很好,就是不设置系统选中高亮,我们自己写一个背景,然后自己实现高亮状态。
但是我用的背景图是自动拉伸的,设置以后,纵向拖动cell会导致背景图变成拉伸前的原图,效果很糟糕。而且有时候点击cell也会直接加载原图,需要reload cell再设置selected才有好的效果。所以我试着选择设置背景色,结果效果很好。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{    [super setSelected:selected animated:animated];        [self setSelectionStyle:UITableViewCellSelectionStyleNone];    if (selected) {        [self setBackgroundColor:[UIColor colorWithRed:0x79 / 255.f green:0xD8 / 255.f blue:0xFA / 255.f alpha:1.f]];    } else {        [self setBackgroundColor:[UIColor whiteColor]];    }}

好了,解决了,有人说这种光标不显示的现象是系统的问题。另外我试了一下,同样的设置,如果把UITextView换成UITextField,就会有光标。崩溃了。。。算了,实在找不出原因了,暂且把锅甩给系统好了。
最后总结,以后如果一个效果找不到系统的实现方法,我们就自己实现好了。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表