以下代码(在viewDidLoad中调用)会导致完全红色的屏幕.我希望它是一个完全绿色的屏幕.它为什么是红色的?我怎么能把它全部变成绿色?
UIScrollView* scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
UIView* contentView = [UIView new];
contentView.translatesAutoresizingMaskIntoConstraints = NO;
contentView.backgroundColor = [UIColor greenColor];
[scrollView addSubview:contentView];
NSDictionary* viewDict = NSDictionaryOfVariableBindings(scrollView,contentView);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|" options:0 metrics:0 views:viewDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|" options:0 metrics:0 views:viewDict]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:0 views:viewDict]];
[scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[contentView]|" options:0 metrics:0 views:viewDict]];
Run Code Online (Sandbox Code Playgroud) 我有一个表格视图和里面的单元格.该单元格包含三个标签:标题标签和两个彼此相同的标签.有时,如果它们不包含数据并将"Header label"的"Top space to container"更改为"Center Y to container",我需要隐藏下面的两个标签.当然,当两个标签包含数据时,还原约束.这是一个简单的演示项目的屏幕截图,仅用于展示这个想法:

更新 Max MacLeod的回答指向了正确的方向.诀窍是在隐藏第一个和第二个标签时向下推标头标签.因此,我们需要为第一个和第二个标签设置底部空间到容器视图,而不是为标题标签的容器视图创建顶部空间.隐藏/取消隐藏应该通过高度出口(第一个和第二个标签的高度约束)并将它们的常量值设置为零(并在取消隐藏时设置返回值)来完成.我还将源代码示例上传到Github.
我在自己的UI设计中使用了自动布局的故事板.在android中基本上有三种不同的属性,如Visible,Invisible和不见了.
例如:
1) android:visibility="gone" // used to hide the control and as well as space
(or)
CONTROLNAME.setVisibility(View.GONE);
2) android:visibility="invisible" // used to hide the control but it will take space
(or)
CONTROLNAME.setVisibility(View.INVISIBLE);
Run Code Online (Sandbox Code Playgroud)
在IOS,
Objective-C的
1) ?
2) [CONTROLNAME setHidden:TRUE]; // used to hide the control but it will take space
Run Code Online (Sandbox Code Playgroud)
迅速
1) ?
2) CONTROLNAME.isHidden = true // used to hide the control but it will take space
Run Code Online (Sandbox Code Playgroud)
作为IOS 消失的行为我从谷歌搜索但我无法找到解决方案.
我的视图控制器中有几个视图,当检测到向下滑动时向下移动,然后在检测到向下滑动时向下移动.我通过使用CGRectOffset调整y原点来强制移动视图.我现在已经对IB的观点应用了约束,我不确定最好的方法是移动视图,以便它们最终在iphone 5,6和6+上的正确位置.
目前我正在做这样的事情:
[self.view layoutIfNeeded];
self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant +338;
[UIView animateWithDuration:5
animations:^{
[self.view layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)
使用比率更改常量是否更好?因此,对于上面的约束,而不是使用338,这样做会更好:
self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant + (self.panView.frame.size.height/1.680);
//self.panView.frame.size.height = 568
//(568/1.680) = 338
Run Code Online (Sandbox Code Playgroud) 当其中一个被隐藏时,我想制作可调整大小的视图。已经在以下链接中针对 iOS 提出了该问题。我想为Android制作它。任何帮助表示赞赏。我想在将可见性隐藏到计数和折扣后为三个 TextViev 添加可用空间:位置、统计名称、价格
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="30dp"
android:weightSum="100" >
<TextView
style="@style/list_titles_style"
android:layout_weight="10"
android:text="@string/position" />
<TextView
style="@style/list_titles_style"
android:layout_weight="35"
android:paddingLeft="15dp"
android:text="@string/stat_name" />
<TextView
style="@style/list_titles_style"
android:layout_weight="20"
android:text="@string/price" />
<TextView
android:id="@+id/count_label"
style="@style/list_titles_style"
android:layout_weight="10"
android:visibility="invisible"
android:text="@string/count" />
<TextView
android:id="@+id/discount_label"
style="@style/list_titles_style"
android:visibility="invisible"
android:layout_weight="25"
android:text="@string/discount" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="1dp"
android:background="@drawable/border_layout"
android:orientation="horizontal" >
<ListView
android:id="@+id/stats_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_marginBottom="5dp"
android:divider="@drawable/list_divider"
android:dividerHeight="2dp"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingTop="0dp" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的风格是:
<style name="list_titles_style">
<item name="android:layout_width">0dp</item> …Run Code Online (Sandbox Code Playgroud) 许多关于动画 AutoLayout 约束的教程建议更新约束的常量属性,然后调用layoutIfNeeded()动画块。
我的情况有点棘手。我有一个包含 3 个子视图的视图。这个 superview 的高度不是固定的 - 它被计算为其子视图的高度总和。在某些情况下,我要求这 3 个子视图之一切换其高度(它在 0 和 30 之间变化,即我想平滑地隐藏和显示它)。代码类似于:
// In superview
subview1.isVisibleInContext = !subview1.isVisibleInContext
// In subview
class MySubview: UIView {
@IBOutlet var heightConstraint: NSLayoutConstraint!
var isVisibleInContext = false {
didSet {
updateHeight()
}
}
func toggleHeight() {
let newHeight = isVisibleInContext ? 30 : 0
layoutIfNeeded()
heightConstraint.constant = newHeight
UIView.animate(withDuration: 0.8) {
self.layoutIfNeeded()
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这并不像我期望的那样工作。我可以看到我的子视图高度的平滑变化,但是在我为我的子视图高度约束设置新值后,我的超级视图的高度会立即重新计算。
我想看到超级视图的高度随着其子视图的增长或减少而逐渐增加/减少。
请有人指出我正确的方向。任何帮助表示赞赏。
非常感谢!
uiviewanimation ios autolayout nslayoutconstraint ios-autolayout
autolayout ×5
ios ×5
objective-c ×3
android ×2
swift ×2
hidden ×1
ios6 ×1
uiscrollview ×1
view ×1
xml ×1