Disable Horizontal Scrolling to UITextView Programmatically

╄→гoц情女王★ 提交于 2019-12-04 02:35:34

问题


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 programmatically I can't find a way to do this, I've googled it as well but the most I got was this:

How to stop the horizontal scrolling programmatically?

and it is not working. Any hints?

self.textViewOpenedRect = CGRectMake(20, 20, 280, 130);
self.textView = [[UITextView alloc] initWithFrame: self.textViewOpenedRect];
self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5);
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor clearColor];
self.textView.contentSize = CGSizeMake( self.textView.frame.size.height, self.textView.contentSize.height);

self.textView.alwaysBounceHorizontal = NO;
self.textView.bounces = NO;

UPDATE: apparently the problem is because of that line of code:

self.textView.contentInset = UIEdgeInsetsMake(5, 5, 5, 5);

wich I use to have a bit of space between the frame and the text. . .am I doing it properly?


回答1:


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;



回答2:


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);



回答3:


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.



来源:https://stackoverflow.com/questions/9657746/disable-horizontal-scrolling-to-uitextview-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!