Unable to Scroll in Scrollview

Unable to scroll in scroll view

First turn off AutoLayout - click on scrollview and uncheck Use AutoLayout

In your .h file add the UIScrollViewDelegate for example:

@interface myViewController : UIViewController <UIScrollViewDelegate>

In your .m file in viewDidLoad set

self.myScrollView.delegate = self;
self.myscrollView.scrollEnabled = YES;
self.myscrollView.contentSize=CGSizeMake(320.0, 1094.0);

keep Bounces ON and set scrollview frame 320 X 960

unable to scroll in scrollView

I made an expo snack for your problem and examined it. I think I solved it. take a look at it:

This is the link to the expo snack

To describe about it:

As I found out, your problem was that the ScrollView wasn't changing height in any situations, even by giving more height or giving paddingBottom.

so I wrapped the ScrollView in a View tag and gave this styling to it:

scrollViewWrapper: {
width: '100%',
minHeight: '100%',
paddingBottom: 1.3*keyboardHeight
}

In this styling, you see parameter keyboardHeight, which is a state that I'm setting in the componentDidMount with below codes:

import {Keyboard} from 'react-native';

state = {
keyboardHeight: 0,
}

componentDidMount() {
this.keyboardDidShowListener = Keyboard.addListener(
'keyboardDidShow',
this._keyboardDidShow
);
this.keyboardDidHideListener = Keyboard.addListener(
'keyboardDidHide',
this._keyboardDidHide
);
}
_keyboardDidShow = (e) => {
this.setState({
keyboardHeight: e.endCoordinates.height,
});
};

I have to mention that you cannot move the style of paddingBottom: 1.3*keyboardHeight outside of the class component and place it in styles object, because the keyboardHeight is a state, it can be known just inside the component.

I suggest you to take a look at my code in expo snack to understand my descriptions better.

I hope this solve your problem.

Edit for functional components

use below code to detect keyboard height while writing functional components:

const [keyboardHeight, setKeyboardHeight] = useState(0)
const [scrollViewVisibility, setScrollViewVisibility] = useState(false)
useEffect(() => {
Keyboard.addListener("keyboardDidShow", _keyboardDidShow);

// cleanup function
return () => {
Keyboard.removeListener("keyboardDidShow", _keyboardDidShow);
};
}, []);
const _keyboardDidShow = (e) => {
console.log("============in keyboardDidShow")
setKeyboardHeight(e.endCoordinates.height)
};

I edited the expo snack too. you can take a look at it to see a working example.

UIScrollView won't scroll!

You need to set UIScrollView.contentSize to match the total scrollable size, which is your subview frame size in this case.

Cannot Scroll UIScrollView

Is the content size larger than the scrollview's frame? That's the only way it'll scroll.

UIScrollView not able to scroll?

You can't scroll because scrollView.contentSize isn't bigger then your view.

If you want make your screen scrollable you should set scrollView.contentSize.

Put scrollView.contentSize = CGSize(width: 1000, height: 1000) before view.addSubview(scrollView) to set your scrollable area to 1000x1000.

Generally scrollView.contentSize should be calculated base on UI elements inside UIScrollView e.g. size of 10 images with space between them.

UIScrollView not scrolling

It's always good to show a complete working code snippet:

// in viewDidLoad (if using Autolayout check note below):

UIScrollView *myScrollView;
UIView *contentView;
// scrollview won't scroll unless content size explicitly set

[myScrollView addSubview:contentView];//if the contentView is not already inside your scrollview in your xib/StoryBoard doc

myScrollView.contentSize = contentView.frame.size; //sets ScrollView content size

Swift 4.0

let myScrollView
let contentView

// scrollview won't scroll unless content size explicitly set

myScrollView.addSubview(contentView)//if the contentView is not already inside your scrollview in your xib/StoryBoard doc

myScrollView.contentSize = contentView.frame.size //sets ScrollView content size

I have not found a way to set contentSize in IB (as of Xcode 5.0).

Note:
If you are using Autolayout the best place to put this code is inside the -(void)viewDidLayoutSubviews method .

ScrollView not scrolling at all

Answer: the ScrollView is not working when used as the root element of an XML layout. It has to be wrapped inside a LinearLayout.

Solution then :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >

<LinearLayout android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

</LinearLayout>
</ScrollView>
</LinearLayout>

Unable to scroll even after using Scrollview

I have found the solution for the above issue
changing the blogImage height from 60% -> 600
fixed the issue and now scroll is working
thanks to @wojtek2939 for helping....



Related Topics



Leave a reply



Submit