React Native: vertical centering when using ScrollView
I solved this issue with flexGrow instead of flex
<ScrollView contentContainerStyle={{ flexGrow: 1, justifyContent: 'center' }}>
This makes the content container stretch to fill the ScrollView but does not restrict the maximum height, so you can still scroll correctly when the content extends the bounds of the ScrollView
Center element horizontally in visible area in a scroll view
The only solution I came up with is calculate it manually. Here's the code (with React):
const rowRef = useRef<HTMLDivElement>(null);
let scrollLeft: any = null;
let throttleScrollEventActive = false;
function handleScrollEvent(event: any, element: any) {
if (throttleScrollEventActive) return;
window.requestAnimationFrame(function () {
const left = element.getBoundingClientRect().left;
if (scrollLeft != left) {
scrollLeft = left;
setVisibleButtonWidth();
}
throttleScrollEventActive = false;
});
throttleScrollEventActive = true;
}
useEffect(() => {
setVisibleButtonWidth();
const el = rowRef.current;
if (!el) return;
window.addEventListener('scroll', (e) => handleScrollEvent(e, el), true);
return () => {
window.removeEventListener('scroll', (e) => handleScrollEvent(e, el), true);
};
}, [rowRef.current]);
function setVisibleButtonWidth() {
const el = rowRef.current;
if (!el) return;
const parent = el.parentElement;
if (!parent) return;
const clientWidth = parent.clientWidth;
const rect = el.getBoundingClientRect();
const left = rect.left;
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);
let width = null;
if (rect.left < 0 && rect.right < vw) {
width = rect.right;
} else if (rect.left < 0 && rect.right > vw) {
width = vw;
} else if (left + clientWidth > vw) {
width = vw - left;
}
if (width != null) {
el.style.maxWidth = `${width}px`;
}
}
SwiftUI ScrollView won't center content with GeometryReader
Move GeometryReader
outside of VStack
:
struct ChartView: View {
var body: some View {
GeometryReader { geo in
VStack(alignment: .center) {
ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(0..<self.chartSettings.getChartYears(), id:\.self) { index in
ColumnView()
}
}
}
.background(Color.gray)
.frame(maxHeight: geo.size.height/2)
//Chart Legend
HStack{
Rectangle()
.frame(width: 10, height: 10)
.foregroundColor(Color.init(#colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)))
Text("First Value")
.font(.system(size: 12))
Rectangle()
.frame(width: 10, height: 10)
.foregroundColor(Color.init(#colorLiteral(red: 0.2263821661, green: 0.4659538441, blue: 0.08977641062, alpha: 1)))
Text("Second Value")
.font(.system(size: 12))
}
}
}
}
}
Note: horizontal scroll view content always start from left (it is not alignment). If you want just center HStack
on screen - remove ScrollView
.
HorizontalScrollView with centered elements
use the gravity attribute :
android:layout_gravity="center_horizontal"
EDIT : 03.30 :
I found it ! only had to set the gravity to the upper LinearLayout : all you need to do is add a little padding/margins to make the textviews more comfortable to read !
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MenuB" >
</TextView>
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MenuB" >
</TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MenuB" >
</TextView>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MenuB" >
</TextView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MenuA" >
</TextView>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MenuB" >
</TextView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
keep me posted !
Related Topics
Extract Reality Composer Scene for Arquicklook
Avaudiopcmbuffer Built Programmatically, Not Playing Back in Stereo
Tvos Button Inside Navigationlink Is Not Working
Xcode 6.1 Swift Extensions - Sourcekit Service Crash
Cannot Authenticate User for Aws Appsync with Swift Sdk
Wrong Image Orientation After Displaytransform Call
How to Loop Over The Output of a Publisher with Combine
Trouble Calling a Method in an Init
List Inside Scrollview Is Not Displayed on Watchos
Swift Set UIbutton Setbordercolor in Storyboard
Swift - Nsdate - Remove Part of Date
What Is The Reason to Store Subscription into a Subscriptions Set
How to Horizontally Center Content of Horizontal Scrollview
Root Class of All Classes in Swift
Closure Identity in Swift: Unregister Observing Closure
How to Do Imageview.Startanimating() with Completion in Swift