Disable Horizontal Scrolling to UITextView Programmatically

后端 未结 3 1126
迷失自我
迷失自我 2021-01-16 03:06

I\'m looking for a way to disable horizontal scrolling of a UITextView programmatically, it was easy via the Interface Builder, but since I\'m designing the view programmati

相关标签:
3条回答
  • 2021-01-16 03:37

    With custom insets i found the best solution for me is to subclass the UITextView and then in the layoutSubviews call the following code.

    -(void) layoutSubviews
    {
        // always keep content offset at x = 0
        self.contentOffset = CGPointMake(0, self.contentOffset.y );
        [super layoutSubviews];
    }
    

    Hope this helps someone.

    0 讨论(0)
  • 2021-01-16 03:50

    Just to add a bit here, that "tiny bit of scrolling left and right" even after you disable horizontal scrolling is caused by the ContentInsets. The following disables that:

    self.textView.contentInset = UIEdgeInsetsMake(5, 0, 5, 0);
    
    0 讨论(0)
  • 2021-01-16 03:55

    This should help... UITextView is a subclass of UIScrollView, so this is pretty easy to implement.

    mytextView.contentSize = CGSizeMake(mytextView.frame.size.height,mytextView.contentSize.height);
    
    mytextView.showsHorizontalScrollIndicator = NO;
    
    0 讨论(0)
提交回复
热议问题