How to disable horizontal scrolling of UIScrollView?
You have to set the contentSize
property of the UIScrollView
. For example, if your UIScrollView
is 320 pixels wide (the width of the screen), then you could do this:
CGSize scrollableSize = CGSizeMake(320, myScrollableHeight);
[myScrollView setContentSize:scrollableSize];
The UIScrollView
will then only scroll vertically, because it can already display everything horizontally.
Disable horizontal scroll in UIScrollView with autolayout
1) Horizontal scroll enables automatically when the content width in scrollView more than width of scrollView. Therefore, in order to avoid horizontal scrolling is necessary to make width of the content inside scrollView less than or equal to scrollView width.
Leading space
and trailing space
can't set specific width to views, they just stretch them. In regular views, they no stretch for more than width of view, but scrollView is a special view, actually, with an infinite content width. Therefore, trailing space
and leading space
constraints in scrollView change the width of views to their maximum possible values (In case with UILabel you can see resize to fit the text).
To avoid horizontal scrolling, you need to set specific width of each view, less than or equal to scrollView width. Specific width of views may be set with width constraints
.
Instead of setting each view width, much better to add a view–container and set width to it, and inside it place the views as needed.
Views hierarchy:
View
-> ScrollView
-> ContainerView
-> UILabel
-> UILabel
-> ... other views that you need
Autolayout constraints:
ScrollView
-> leading space to View : 0
-> trailing space to View : 0
-> top space to View : 0
-> bottom space to View : 0
Container View
-> leading space to ScrollView : 0
-> trailing space to ScrollView : 0
-> top space to ScrollView : 0
-> bottom space to ScrollView : 0
-> width equal to ScrollView : 0
To set width equal constraint
ctrl+drag from containerView
to scrollView
.
2) Vertical scroll is dependent on the total height of content. Blank space can be, if the last element inside containerView
has a large value of the bottom space to superview
.
Or do you mean bounce effect? You can disable vertical bounce of scrollView.
Disabling Horizontal Scrolling from UIScrollView Swift
Like this,
Swift 4.0
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.x>0 {
scrollView.contentOffset.x = 0
}
}
And, you can set this property:
scrollImg.isDirectionalLockEnabled = true
Disable Horizontal Scrolling of UIScrollView AutoLayout Resizable ScrollView
Content UIView
width should be equal to the width of UIScrollView
's superview for instance, not UIScrollView
itself.
How can I disable horizontal scrolling and page will change on click in paging using UIScrollView?
You can use UICollectionView with paging enabled and inside you can implement UIScrollView with vertical scroll enable. Disable the UICollectionView scroll and change the index collection cell on click using below code:
accepted
New, Edited Answer:
Add this in viewDidLayoutSubviews
SWIFT
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let indexPath = IndexPath(item: 12, section: 0)
self.collectionView.scrollToItem(at: indexPath, at: [ .centeredHorizontally], animated: true)
}
Normally, .centerVertically is the case
Objective-C
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:12 inSection:0];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
}
How do I enable horizontal scrolling and disable vertical scrolling in UIWebview?
Enable Horizontal scrolling and disable Vertical scrolling:
myWebView.scrollView.delegate = self;
[myWebView.scrollView setShowsVerticalScrollIndicator:NO];
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y > 0 || scrollView.contentOffset.y < 0 )
scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
}
the 2nd line there will hide the vertical scroll indicator. Checking if "scrollView.contentOffset.y < zero" will disable the bouncing when you attempt to scroll downwards. You can also do: scrollView.bounces=NO to do the same thing!!
By just looking at Rama Rao's answer i can tell that his code will reset the scrollView to (0.0) the moment you try to scroll vertically, thereby moving you away from your horizontal position, which is not good.
Enable vertical scrolling and disable Horizontal scrolling:
myWebView.scrollView.delegate = self;
[myWebview.scrollView setShowsHorizontalScrollIndicator:NO];
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x > 0)
scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);
}
PEACE
Related Topics
What Is a Monospace Font in iOS
iOS 9 Uitableview Separators Insets (Significant Left Margin)
iOS Push Notification Banner Shown Twice for a Single Push
When Exactly Do Implicit Animations Take Place in iOS
Change Uiimageview Size to Match Image with Autolayout
How to Play Video Stream with Mpmovieplayercontroller in iOS
Why Can't I Call the Default Super.Init() on Uiviewcontroller in Swift
Mfmailcomposeviewcontroller in iOS 7 Statusbar Are Black
Xcode Cannot Run on the Selected Destination
@Property/@Synthesize Equivalent in Swift
Found an Unexpected MACh-O Header Code: 1918975009 in Xcode 6
How to Calculate the Uilabel Height Dynamically
Where Do I Find iOS Obj-C Code to Scan and Connect to Wifi (Private API)
Apple MACh-O Linker Warning "Directory Not Found for Option..."
How to Get Server Response Data in Nsurlsession Without Completion Block