iOS评论框动态改变高度

使用kvo监听textView的contentSize改变

    [self.textView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];

- (void)layoutSubviews {
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    self.leftButton.frame = CGRectMake(0, (height-50)/2, 50, 50);
    self.textView.frame = CGRectMake(50, 5, width- 50-80, height-5);
    self.sendButton.frame = CGRectMake(width - 80, 0, 80, height);
    
    self.line1.frame = CGRectMake(0, 0, width, 1);
    self.line2.frame = CGRectMake(self.sendButton.leftX, height *0.2, 1, height*0.6);
    
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"contentSize"])
    {
        if (self.textView.contentSize.height <= 36) {
            self.frame = CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50);
        }else {
            if (self.textView.contentSize.height <= 74) {
                self.frame = CGRectMake(0, SCREEN_HEIGHT-self.textView.contentSize.height-10, SCREEN_WIDTH, self.textView.contentSize.height+10);
            }else {
                self.frame = CGRectMake(0, SCREEN_HEIGHT-74, SCREEN_WIDTH, 74);
            }
        }
    }
}

你可能感兴趣的:(iOS评论框动态改变高度)