Showing posts with label ScrollView. Show all posts
Showing posts with label ScrollView. Show all posts

Thursday, February 21, 2019

Swift Scrollview

We want a view can scroll horizonal and vertical.
Declare one more Scrollview and a Label.
var la:UILabel!
var scrollView: UIScrollView!
Set label width and height lager than the dimension you need. For example, you need 500 width and 800 height, let set label 520, 820 like this.
la = UILabel(frame: CGRect(x: 0, y: 0, width: 520, height: 820))
Add this line for Scrollview.
scrollView = UIScrollView(frame: view.bounds)
After that, put all view like button, image, text in to scrollview.

scrollView.addSubview(tableview)
 scrollView.addSubview(bu1)
scrollView.addSubview(bu2)
scrollView.addSubview(image)
Last, add this two lines.
 scrollView.contentSize = la.bounds.size

 view.addSubview(scrollView)

ScrollView


We use ScrollView to scroll a layout longer than screen height.
To scroll part of screen, we use this.
<ScrollView
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:fillViewport="true" >
        <LinearLayout
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical" >
    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@null"
        android:src="@drawable/abc" />
 <ImageView      
        android:layout_width="500dp"
        android:layout_height="230dp"
        android:layout_gravity="center_horizontal"
        android:contentDescription="@null"
        android:src="@drawable/abc" />
 </LinearLayout>
  </ScrollView>
We must put all views in a LinearLayout with orientation vertical.
To scroll a whole screen, we wrap it in ScrollView like this.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
</ScrollView>
We use a LinearLayout vertical inside.
To horizontal scroll.
<HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fillViewport="true" >
</HorizontalScrollView>
If we want both vertical and horizontal scroll, use them like this.
<HorizontalScrollView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
  android:fillViewport="true" >
 <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:orientation="vertical" >  
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
 <LinearLayout
  android:layout_width="fill_parent"
 android:layout_height="wrap_content"
  android:orientation="vertical" >
  <ImageView
 android:id="@+id/img"
 android:layout_width="match_parent"
android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:contentDescription="@null"
 android:src="@drawable/abc" />
 <ImageView      
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:contentDescription="@null"
 android:src="@drawable/abc" />
 </LinearLayout>
 </ScrollView>
 </LinearLayout>
 </HorizontalScrollView>
If we want a TextView can scroll, put it inside a scrollview
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> 
<TextView
 android:id="@+id/tv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:text=" Scroll TextView"
 android:textColor="#800000"
 android:textSize="26sp" />
</ScrollView>