【问题标题】:Constraints autolayout for uitableview and uiview约束 uitableview 和 uiview 的自动布局
【发布时间】:2017-12-24 03:02:04
【问题描述】:

我面临一个约束自动布局问题,即我在上部有一个 UITableView,在下部有一个 UIView。当 UITableView 展开 tableView 部分时,我希望底部的 UIView 自动向下移动,当折叠 tableView 部分时,底部的 UIView 会自动向上移动。

下面是我的storyboard设计,绿色部分是UITableView,红色部分是UIView。

You can get my code from here

非常感谢您的帮助。

【问题讨论】:

  • 你不能让 UIView 成为你的 tableview 的一部分(单元格或页脚视图)然后它会随着你的其他 table view 单元格移动
  • @Tj3n 其实下面还有一些其他的UIView。
  • @NurII 是不是在部分添加一行时自动调整(扩展)?只要您的底部视图的顶部刷新到 tableview 的底部

标签: ios objective-c xcode uitableview uiview


【解决方案1】:

添加你的tableview高度约束的IBOutlet。

control + 拖放到视图控制器。你会得到这样的财产。

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

然后在切换表格视图时调用它。 contentSize 将计算 tableview 高度并将高度更新为 tableviewHeightConstraints 约束。

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

【讨论】:

  • 错误是“@property (nonatomic, strong) NSLayoutConstraint * tableviewHeightConstraints;”而不是“@property (nonatomic, strong) IBOutlet NSLayoutConstraints * tableviewHeightConstraints;” ?
  • 是的。更新。请立即检查。你设法让它工作了吗?
  • 嗨,巴拉,它得到了回应。但是,当我单击该部分展开和折叠时。结果变得相反。我已经将我的代码上传到这个链接中,你能帮我看看吗? drive.google.com/open?id=0B3e7l6RGD-rnMmFDZi1LOS1MMkU
  • 您是否将最后一个子视图的 bottom constraint 添加到其位于您的 tableview 下方的 superview 中?。
  • 感谢 Bala 的帮助,它现在非常适合我。
【解决方案2】:

尝试使用ViewForHeaderSection: title1, title 2... 是 tableview 标题部分。当展开 numberofRowinsection = 1 并在折叠时 = 0 - 您的 uiview 是部分中的 uitableview 单元格。
单击剖面视图时重新加载剖面。

【讨论】:

    【解决方案3】:

    不要担心,要快乐。我已经为你制定了解决方案。只需简单地考虑一下您的布局,然后在下面的陡峭处流淌:

    1.你的父布局是ConstraintLayout,
    2.其次取一个RelativeLayout,
    3.然后取另一个 ConstraintLayout 和
    4.最后添加你的“ExpandableListView”和你的UI元素

    你也可以看下面的代码:

    <?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>
    

    或者您也可以使用 Google 链接: https://drive.google.com/open?id=0B-yo9VvU7jyBUHN6RHVXQmUtVU0

    【讨论】:

    • 但这不是 iOS
    猜你喜欢
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 2021-01-14
    相关资源
    最近更新 更多