UITextView滚动无法正常工作

时间:2016-06-20 19:15:11

标签: ios scroll uitextview

在以下代码中,在UITextView中滚动不能用于messageLabel sizeDictionary是一个字典,其中包含根据text.i将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];
    }
}

enter image description here

编辑:添加

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

enter image description here

1 个答案:

答案 0 :(得分:2)

很容易,然后您的问题在于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 < / p>

我希望这有助于你