UIStoryboard how to replace constraints programmatically?(UIStoryboard 如何以编程方式替换约束?)
问题描述
我在情节提要中布置了一个视图控制器,并启用了自动布局,我正在寻找一种方法来更改约束以允许我的视图旋转为横向并重新排列屏幕上的按钮.当我尝试下面的代码时,我收到大约两打无法满足约束,打破约束......"的消息,我无法真正解码.
I have a view controller laid out in a storyboard with autolayout enabled and am looking for a way to change constraints to allow for my view to rotate into landscape and rearrange buttons on the screen. When I try the code below, I get about two dozen "unable to satisfy constraints, breaking constraint..." messages that I cannot really decode.
有没有办法用我以编程方式指定的约束动态替换情节提要中的约束?我想完全覆盖我在情节提要中定义的按钮布局.
Is there a way to dynamically replace constraints from a storyboard with constraints that I specify programmatically? I want to completely override the layout of the buttons that I defined in a storyboard.
-(void)updateViewConstraints
{
[super updateViewConstraints];
self.loginButton.translatesAutoresizingMaskIntoConstraints = NO;
self.getStartedButton.translatesAutoresizingMaskIntoConstraints = NO;
self.takeTourButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.loginButton removeConstraints:self.loginButton.constraints];
[self.getStartedButton removeConstraints:self.getStartedButton.constraints];
[self.takeTourButton removeConstraints:self.takeTourButton.constraints];
one = self.loginButton;
two = self.getStartedButton;
three = self.takeTourButton;
NSDictionary *metrics = @{@"height":@50.0,@"width":@100.0,@"leftSpacing":@110.0};
NSDictionary *views = NSDictionaryOfVariableBindings(one,two,three);
[self.view removeConstraints:activeTestLabelConstraints];
[activeTestLabelConstraints removeAllObjects];
if(isRotatingToLandscape)
{
[self registerConstraintsWithVisualFormat:@"|-[one(two)]-[two(three)]-[three]-|" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:metrics views:views];
[self registerConstraintsWithVisualFormat:@"V:[one(height)]-|" options:0 metrics:metrics views:views];
}else
{
[self registerConstraintsWithVisualFormat:@"|-leftSpacing-[one(width)]" options:0 metrics:metrics views:views];
[self registerConstraintsWithVisualFormat:@"[two(width)]" options:0 metrics:metrics views:views];
[self registerConstraintsWithVisualFormat:@"[three(width)]" options:0 metrics:metrics views:views];
[self registerConstraintsWithVisualFormat:@"V:[one(height)]-[two(75)]-[three(100)]-|" options:NSLayoutFormatAlignAllCenterX metrics:metrics views:views];
}
}
更新了 Rob 的答案,这是我使用的移除约束的方法
Updated with Rob's answer, here's the method to remove constraints that I use
-(void)removeConstraintForView:(UIView*)viewToModify
{
UIView* temp = viewToModify;
[temp removeFromSuperview];
[self.view addSubview:temp];
}
推荐答案
听起来您只想删除视图上的所有约束.由于视图上的约束通常由视图的祖先持有,因此如何轻松删除所有约束并不明显.但其实很简单.
It sounds like you want to just remove all the constraints on a view. Since constraints on a view are often held by an ancestor of the view, it's not obvious how to remove all of the constraints easily. But it is actually pretty easy.
从视图层次结构中移除一个视图会移除该视图与其外部的其他视图之间的任何约束.因此,只需从其父视图中删除您的视图,然后将其添加回来:
Removing a view from the view hierarchy removes any constraints betwixt the view and other views outside of it. So just remove your view from its superview and then add it back:
// Remove constraints betwixt someView and non-descendants of someView.
UIView *superview = someView.superview;
[someView removeFromSuperview];
[superview addSubview:someView];
如果在 someView
及其后代之间存在任何约束,则这些约束可能由 someView
本身持有(但不能由 someView<的任何后代持有/代码>).如果你也想删除这些约束,你可以直接这样做:
If there are any constraints betwixt someView
and its descendants, those constraints might be held by someView
itself (but cannot be held by any descendants of someView
). If you want to remove those constraints also, you can do so directly:
// Remove any remaining constraints betwixt someView and its descendants.
[someView removeConstraints:[someView constraints]];
这篇关于UIStoryboard 如何以编程方式替换约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIStoryboard 如何以编程方式替换约束?
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01