Constraints autolayout for uitableview and uiview

2019-07-29 11:16发布

问题:

I'm facing a constraints autolayout issue which is I have a UITableView at upper and UIView at lower. When the UITableView expand the tableView section, I want the UIView at the bottom shift down automatically, when collapse the tableView section, the UIView at bottom will move up automatically.

Below is my storyboard design, on the green sector is UITableView, on the red sector is UIView.

You can get my code from here

Your help is much appreciated.

回答1:

Add the IBOutlet of height constraints of your tableview.

control + drag and drop to the view controller. You will get the property like this.

@property (strong, nonatomic) IBOutlet NSLayoutConstraint * tableviewHeightConstraints;

Then call this one whenever you toggle the tableview. contentSize will calculate the tableview height and update the height to the tableviewHeightConstraints constraints.

CGFloat height = MIN(self.view.bounds.size.height, self.tableView.contentSize.height);
self.tableviewHeightConstraints.constant = height;
[self.view layoutIfNeeded];


回答2:

Try use ViewForHeaderSection: title1, title 2... is tableview header section. When expand numberofRowinsection = 1 and = 0 when collapse - your uiview is uitableview cell in section.
Reload section when click section view.



回答3:

don't worry and be happy. I have make a solution for you. Just simple thinking about your Layout and flow the steep below:

1.Your parent layout is ConstraintLayout,
2.Secondly take a RelativeLayout,
3.Then take another ConstraintLayout and
4.Finally add your "ExpandableListView" and your UI element

You can also see the code below:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#EACB3B"
    tools:context="com.example.uitableview.MainActivity">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            tools:layout_constraintTop_creator="1"
            tools:layout_constraintRight_creator="1"
            tools:layout_constraintBottom_creator="1"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_constraintLeft_creator="1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/lvID"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">


                        <ImageView
                            android:id="@+id/imgView"
                            android:layout_width="wrap_content"
                            android:layout_height="234dp"
                            android:src="@drawable/mango"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            android:layout_marginTop="8dp"
                            app:layout_constraintTop_toBottomOf="@+id/expLv"
                            app:layout_constraintHorizontal_bias="0.0" />

                        <ExpandableListView
                            android:id="@+id/expLv"
                            android:layout_width="352dp"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="16dp"
                            android:orientation="vertical"
                            app:layout_constraintLeft_toLeftOf="parent"
                            app:layout_constraintRight_toRightOf="parent"
                            app:layout_constraintTop_toTopOf="parent" />
                </android.support.constraint.ConstraintLayout>

        </RelativeLayout>
</android.support.constraint.ConstraintLayout>

Or you can also flow the Google Link: https://drive.google.com/open?id=0B-yo9VvU7jyBUHN6RHVXQmUtVU0