【问题标题】:UITextView scrolling is not workingUITextView 滚动不起作用
【发布时间】:2016-06-20 19:15:11
【问题描述】:

以下代码中的 messageLabel 在 UITextView 中滚动不起作用 sizeDictionary 是一个字典,其中包含根据文本计算的所有标签/文本视图的大小。我已将 scrollEnabled 以及 userInteractionEnabled 属性设置为 true。

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.view.backgroundColor = [UIColor colorWithWhite:0.91 alpha:1];
    self.messageLabel = [[UITextView alloc] init];

    if(self.feed.message){
        self.messageLabel.text = self.feed.message;
    }else if (self.feed.story){
        self.messageLabel.text = self.feed.story;
    }
    self.messageLabel.font = [UIFont fontWithName:@"Droid Sans" size:15];
    self.messageLabel.editable = NO;
    self.messageLabel.dataDetectorTypes = UIDataDetectorTypeAll;
    self.messageLabel.scrollEnabled = true;
    self.messageLabel.userInteractionEnabled = true;

    self.messageLabel.textColor = [UIColor colorWithRed:0.078 green:0.094 blue:0.137 alpha:1.00];
self.cardView = [[UIView alloc] init];
self.cardView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
[self.view setTranslatesAutoresizingMaskIntoConstraints:false];
self.cardView.frame = CGRectMake(5, 5 + self.navigationController.navigationBar.frame.size.height, self.view.bounds.size.width - 10, [self.sizeDictionary[@"imageHeight"] floatValue]+ [self.sizeDictionary[@"messageHeight"] floatValue] + [self.sizeDictionary[@"nameHeight"] floatValue] + 20);
self.cardView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.cardView];
self.nameLabel.frame = CGRectMake(10, 15+ self.navigationController.navigationBar.frame.size.height, self.view.bounds.size.width, 51);

self.dateLabel.frame = CGRectMake(15, 45+ self.navigationController.navigationBar.frame.size.height, self.view.bounds.size.width, 25);
self.messageLabel.frame = CGRectMake(10, [self.sizeDictionary[@"nameHeight"] floatValue]  + 20 + self.navigationController.navigationBar.frame.size.height, self.view.bounds.size.width - 20, [self.sizeDictionary[@"messageHeight"] floatValue]);
self.messageLabel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.messageLabel];
[self.view addSubview:self.dateLabel];
[self.view addSubview:self.nameLabel];
if (self.image) {
    self.imageView.frame = CGRectMake(10, [self.sizeDictionary[@"nameHeight"] floatValue] + [self.sizeDictionary[@"messageHeight"] floatValue] + 25 + self.navigationController.navigationBar.frame.size.height , self.view.bounds.size.width - 20, [self.sizeDictionary[@"imageHeight"] floatValue]);
    [self.view addSubview:self.imageView];
    }
}

编辑:添加

self.messageLabel.layer.borderWidth = 1 ;
self.messageLabel.layer.borderColor = [UIColor redColor].CGColor;

【问题讨论】:

  • 文本超过 8 并且被隐藏了?
  • 是的,总共有 10 分。此屏幕只是文本来自 json 的示例。在上一个表中调用 didSelectAtIndexPath 方法时会显示此视图。文本实际上是在 didSelect 方法中传输的
  • 你能添加这个和平代码self.messageLabel.layer.borderWidth = 1 ;self.messageLabel.layer.borderColor = [UIColor redColor].CGColor;吗?并检查您的 messageLabel 框架是否完全可见?
  • 究竟是哪一个?我应该添加整个 viewDidAppear 和 viewDidLoad 吗?还是表格视图?
  • 再次检查我在结束我的评论之前点击“输入”抱歉

标签: ios scroll uitextview


【解决方案1】:

这很容易,那么您的问题在于 messageLabel 的框架,您需要调整您的 messageLabel 框架并将变得可滚动,因为您的 messageLabel.frame.heigth 将小于您的 messageLabel 内容大小

调整这条线

self.messageLabel.frame = CGRectMake(10, [self.sizeDictionary[@"nameHeight"] floatValue]  + 20 + self.navigationController.navigationBar.frame.size.height, self.view.bounds.size.width - 20, [self.sizeDictionary[@"messageHeight"] floatValue]);

您的 self.messageLabel.frame 不能满足您所拥有的对象所需的大小,如果发生这种情况,那么您的 UITextView 将永远不会滚动,因为您的 frame.height 将等于您的 contentSize

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多