UITextView无法滚动。

13
我想在UITextView中显示一个段落。我可以将段落的内容放入文本视图中,但是我无法滚动UITextView(一些内容因为无法滚动而不可见)。
我想要做的是:
CGRect frame = CGRectMake(0, 0, 320, 480);
//allocate view
self.view = [[UIView alloc]initWithFrame:frame];textview = [[UITextView alloc]initWithFrame:CGRectMake(30, 200, 275, 400)];
[textview setFont:[UIFont fontWithName:@"Arial" size:12]];
[textview setScrollEnabled:YES];
[textview setUserInteractionEnabled:NO];
[textview setBackgroundColor:[UIColor clearColor]];
//[textview setText:@"Hi,I'm working fine with this space"];
[self.view addSubview:textview];

在这种情况下,滚动是启用的。有人可以告诉我为什么它不能滚动吗?

3
要么删除将userInteractionEnabled设置为NO的那行代码,要么保留该行代码并添加注释以解释其目的。 - user529758
2
如果你保留 [textview setUserInteractionEnabled:NO];,你认为它会如何工作?你需要将其删除以启用用户交互。 - iDev
1
我不知道为什么你添加了:[textview setUserInteractionEnabled:NO]; 只需将其更改为yes即可。 - Bhavin
在这里,如果您将UserInteraction设置为No,则无法检测到此TextView的用户触摸事件,因此此TextView不会滚动,因此只需将其设置为YES即可正常工作,否则请注释掉此行[textview setUserInteractionEnabled:NO];或删除此行。 - Paras Joshi
8个回答

20

设置[textview setUserInteractionEnabled:TRUE];,如果您不想让键盘弹出,则可以通过实现uitextview委托方法来隐藏键盘 - (void)textViewDidBeginEditing:(UITextView *)textView {[textView resignFirstResponder];}


1
请将以下与编程有关的内容从英语翻译成中文:并在您的代码textview.delegate=self中设置代理,并在您的.h文件中添加UITextViewDelegate代理。 - Ahsan
我实施了你们提供的答案,但是对我来说没有任何作用。 - bapi
它没有滚动,键盘也可见。我尝试了所有可能的方法。是的,我将其更改为“是”。有没有办法使垂直和水平滚动启用? - bapi
我认为你的内容不足以启用滚动。要滚动文本视图,您应该输入非常长的段落,如果段落的内容被隐藏,则可以滚动。 - Ahsan
使用这段代码没问题,我已经测试过了,它能正常工作。-(BOOL)textViewShouldBeginEditing:(UITextView *)textView { [textView resignFirstResponder]; return NO; }-(void) textViewDidChange:(UITextView *)textView { [textView resignFirstResponder]; } - Ahsan
显示剩余3条评论

7

UITextViewsetUserInteractionEnabled 属性设置为 YES

设置这一行

[textview setUserInteractionEnabled:YES];

替代

[textview setUserInteractionEnabled:NO];

@bapirout 嘿,伙计,这里你将UserInteraction设置为No,那么这个TextView的用户触摸事件就无法被检测到,所以它也无法滚动。只需将其设置为YES即可正常工作,否则请注释掉这行代码[textview setUserInteractionEnabled:NO];或者将其删除.. :) - Paras Joshi
@bapirout不起作用吗??还要先使用此设置框架小,然后再尝试它,只需像CGRect frame = CGRectMake(0, 0, 320, 200)这样给出Rect,尝试一下并发表评论.. - Paras Joshi

4

我有一个只有文本视图且大约有5页文本的ViewController。

以下是Swift 4.0中我的操作:

  1. 非常重要: 确保您在文本视图上设置了自动布局。 在我的 ViewController 中,我将我的约束设置为对边缘检查后的8,8,8,8的文本视图。如果未设置自动布局,则整个内容不会滚动。
  2. 这是我使用的代码:

Swift 4.0

textForPrivacyPolicy.showsVerticalScrollIndicator = true
textForPrivacyPolicy.isEditable = false
textForPrivacyPolicy.isScrollEnabled = true
textForPrivacyPolicy.scrollRangeToVisible(NSMakeRange(0, 0))
textForPrivacyPolicy.setContentOffset(CGPoint(x: 0, y: 0), animated: false)

2
在这个位置添加这行代码:[textview setUserInteractionEnabled:YES];,替换掉原来的代码。
[textview setUserInteractionEnabled:NO];

2

粘贴以下代码

CGRect frame = CGRectMake(0, 0, 320, 480);
//allocate view
self.view = [[UIView alloc]initWithFrame:frame];textview = [[UITextView alloc]initWithFrame:CGRectMake(30, 200, 275, 400)];
[textview setFont:[UIFont fontWithName:@"Arial" size:12]];
[textview setScrollEnabled:YES];
[textview setUserInteractionEnabled:YES];
[textview setBackgroundColor:[UIColor clearColor]];
//[textview setText:@"Hi,I'm working fine with this space"];
[self.view addSubview:textview];

我实施了你们所给出的答案。但是,对我来说都没有用。注意:我的文本视图与分段控件绑定。根据选择的分段控件将显示任何一个段落。 - bapi
@bapirout,请将父视图属性设置为clipSubviews = YES,然后检查并告诉我。 - P.J

2
UserInteractionEnabled 设置为 YES

0

我曾经遇到过同样的问题,我将setscrollable和userinteractionenabled设置为TRUE。结果发现这是一个约束问题。为了解决这个问题,在你的storyboard中点击你的视图控制器(右上角的电池图标),然后进入“Editor”->“Resolve Auto Layout Issues”->“Add Missing Constraints In ”。这对我来说解决了问题!


0

bapi路由,textView有一个用户交互的属性。你已经将它设置为No。如果你想在TextView上执行滚动操作,你必须启用这个属性。

[textview setUserInteractionEnabled:YES];

这会对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接