我正在尝试设置一个面板来显示一些信息.我无法按照自己的意愿将autolay运行起来.
这是我正在尝试做的事情:

我有一个headerView,一个UIImageView和5个标签.我希望标签垂直排列,我希望它们与UIImageView有8个间距.我可以很容易地垂直排列它们:
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[headerView]-[acresLabel]-[addedLabel(==acresLabel)]-[typeLabel(==acresLabel)]-[zonesLabel(==acresLabel)]-[sceneLabel(==acresLabel)]-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(headerView, acresLabel, addedLabel, typeLabel, zonesLabel, sceneLabel)]];
Run Code Online (Sandbox Code Playgroud)
但让它们排列在图像视图之外是一种繁琐/冗长的方式:
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[acresLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, acresLabel)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[addedLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, addedLabel)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[typeLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, typeLabel)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[zonesLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, zonesLabel)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[imageView]-[sceneLabel]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(imageView, sceneLabel)]];
Run Code Online (Sandbox Code Playgroud)
似乎是一种更好的方法,然后为图像视图的每个标签设置约束.我尝试添加NSLayoutFormatAlignAllCenterX到第一个约束,但它将它们全部设置为内容视图的中心X,不会将标签的中心X相互对齐.NSLayoutFormatAlignAllBaseline在垂直使用时给出错误和崩溃.
我将不得不再次为数据做这个(英亩:'价值').
我是一个非常新的iOS开发人员.我喜欢AutoLayout和程序化的UI元素.使用NSLayoutConstraints对我来说非常有意义,调整它们的值有意义的地方在代码中.
然而,我在线创建iOS UI元素的几乎所有信息都使用XIB或Storyboard.有很多关于何时使用其中一个的讨论,包括这里的SO,但我似乎无法找到任何关于为什么某人可能想要跳过它们而只是在代码中完成所有操作的内容.
我在这里缺少一些基本的东西吗?
例如,这是我的代码片段.如果没有XIB或故事板,我不能这样做吗?
UIView *overlayView = [[UIView alloc] initWithFrame:[self getScreenFrameForCurrentOrientation]];
overlayView.opaque = NO;
mHighlightImagePicker.cameraOverlayView = overlayView;
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
float cameraAspectRatio = 4.0 / 3.0;
float imageWidth = floorf(screenSize.width * cameraAspectRatio);
float scale = ceilf(((screenSize.height - 90) / imageWidth) * 100.0) / 100.0;
mHighlightImagePicker.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
mCameraToolBar=[[UIImageView alloc] init];
mCameraToolBar.image =[UIImage imageNamed:@"review_bottom_bar"];
mCameraToolBar.userInteractionEnabled = YES;
mCameraToolBar.hidden = NO;
[mCameraToolBar setTranslatesAutoresizingMaskIntoConstraints:NO];
[overlayView addConstraint:
[NSLayoutConstraint constraintWithItem:mCameraToolBar
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:overlayView
attribute:NSLayoutAttributeBottom
multiplier:1
constant:-70]];
[overlayView addConstraint:
[NSLayoutConstraint constraintWithItem:mCameraToolBar …Run Code Online (Sandbox Code Playgroud) 我只是第一次尝试在笔尖中使用Autolayout.看来我的情况是教科书情况,这NSLayoutConstraints将是完美的解决方案.
我有一个UITableviewCell与UILabel它旁边的图像.图像根据底层数据的属性而变化,它可以改变大小,有时可以隐藏.我的目的是使标签与图像保持一定距离,并在隐藏图像时跨越整个单元格.
我相应地在标签上设置了约束:
即使图像被隐藏,标签仍会被第一个约束缩短.这是预期的行为吗?我通常会在运行时计算和更改标签的宽度,但这似乎是NSLayoutConstraints的用途.
当图像不可见时,如何让它忽略对图像的约束?
考虑以下所需的布局.请注意,这不是"布局字符串",而是图片的代理.所以| = superview边缘, - =空间,[xxx] =视图.
| - [LABEL1] - [标签2 --------------] - [按钮] - |
上面的布局是我想要实现的.用英语讲:
label1 大小适合内容,并从超视图的边缘填充label2label1根据可用的水平空间从边缘填充并展开/收缩button大小适合内容,并从superview的边缘label2和右边缘填充我希望这很清楚.我当前的约束定义为:
[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-8-[label1]-4-[label2]-4-[button]-8-|"
options:0
metrics:nil
views:views];
Run Code Online (Sandbox Code Playgroud)
这导致以下布局:
| - [LABEL1 ----------------------] - [标签2] - [按钮] - |
问:我怎么能修改约束,以便label2按比例尺寸和label和button的大小以适合的内容?
如果有帮助,这里是创建上述样本生成的约束的等效代码.
NSLayoutConstraint *c1 = [NSLayoutConstraint
constraintWithItem:self.label1
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeLeading
multiplier:1.0f
constant:8.0f];
NSLayoutConstraint *c2 = [NSLayoutConstraint
constraintWithItem:self.label2
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.label1
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:4.0f];
NSLayoutConstraint *c3 = …Run Code Online (Sandbox Code Playgroud) 要在屏幕上创建黑色叠加层,我将所有其他视图的视图添加到UIWindow:
UIView *darkOverlayView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
darkOverlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
Run Code Online (Sandbox Code Playgroud)
然后我想将UIImageView置于新的中心darkOverlayView,并尝试如下:
UIImageView *imageFromLink = [[UIImageView alloc] initWithImage:responseObject];
imageFromLink.translatesAutoresizingMaskIntoConstraints = NO;
CGFloat widerThanHeightBy = imageFromLink.bounds.size.width / imageFromLink.bounds.size.height;
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.width]];
[darkOverlayView addConstraint:[NSLayoutConstraint constraintWithItem:imageFromLink attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:[UIScreen mainScreen].bounds.size.height / widerThanHeightBy]];
Run Code Online (Sandbox Code Playgroud)
但每次运行时,我都会收到错误:
视图层次结构不是为约束准备的:当添加到视图时,约束的项必须是该视图(或视图本身)的后代.如果在组装视图层次结构之前需要解析约束,则会崩溃.打破 - [UIView _viewHierarchyUnpreparedForConstraint:]进行调试.2013-12-07 00:59:03.626 Jupiter [58008:70b]查看层次结构未准备好约束.约束:容器层次结构:>在容器层次结构中找不到视图:> - (null)该视图的超级视图:无SUPERVIEW
我怎么能绕过这个?或者我必须使用框架?
我第一次穿过AutoLayout(并没有使用IB /故事板这样做),我试图在UITableViewCell标题标签和细节标签中做一些简单的垂直对齐,像这个:
——————
Padding
Title Label
Spacing (3px)
Detail Label
Padding
——————
Run Code Online (Sandbox Code Playgroud)
我正在努力的部分是让两个"填充"约束在任何时候保持相等.其他一切都有效,但标签位于单元格的顶部或底部.
我实际上正在使用Masonry来实现它,它看起来像这样:
// Constrain the title label at the top of the label container.
[title makeConstraints:^(MASConstraintMaker *make) {
make.top.greaterThanOrEqualTo(self.contentView.top).with.priorityLow();
}];
// Constrain the detail label to the bottom of the label container.
[detail makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleLabel.bottom).with.offset(3).with.priorityHigh();
make.bottom.lessThanOrEqualTo(self.contentView.bottom).with.priorityLow();
}];
Run Code Online (Sandbox Code Playgroud)
我需要make.top来自title约束的make.bottom线和来自约束的线detail始终保持相同的值,以便标题/间距/细节位于中间.
我在这里错过了什么?
在UIView我有一个方法presentView():
public func presentView() {
UIApplication.sharedApplication().delegate?.window??.addSubview(self)
let blurEffect = UIBlurEffect(style: .Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.backgroundColor = UIColor.yellowColor()
addSubview(blurEffectView)
let topConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: blurEffectView, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: blurEffectView, …Run Code Online (Sandbox Code Playgroud) ParentView包含ChildView1和ChildView2。这些子视图的高度不同。
ChildView1比ChildView2高。仅显示一个子视图,例如,如果ChildView1可见,则ChildView2隐藏。ChildView1和ChildView2都使用自定义XIB。
两个子视图都“驱动” ParentView的高度,也就是说,AutoLayout约束已连线,使得ParentView与ChildView1或ChildView2一样高,而没有更高。
问题是隐藏ChildView1并显示ChildView2不会“缩小” ParentView以匹配ChildView2的高度。它保持在较高的子视图ChildView1的高度。
调用sizeToFit()并setNeedsLayout()不会改变任何事情。
隐藏ChildView1时,如何强制ParentView匹配ChildView2的高度?
我一直试图做以下一段时间没有成功..我非常感谢任何建议或提示.
想象一下,UIView它具有固定的高度,并且在其实现的任何超视图中都是水平/垂直的.想象一下,视图具有UILabel控制其宽度的视图.
你现在怎么设置最小宽度UIView?如果UILabel是空的或有2个字符的字符串,我希望UIView至少有50个像素宽度.
这是一个直观的例子:
想象一下UILabel没有文字.然后UIView将有0宽度.这就是我想要解决的问题.
当我尝试创建布局约束时,我了解了NSLayoutAnchor类。他们说:
注意
UIView不提供布局边距属性的锚点属性。相反,layoutMarginsGuide属性提供了一个UILayoutGuide对象,该对象表示这些边距。使用指南的锚点属性创建约束
好。但是同时我创建了没有UILayoutGuide属性的布局边距属性的锚属性。
let inputsContainerView = UIView()
inputsContainerView.backgroundColor = UIColor.white
inputsContainerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(inputsContainerView)
//need x,y,width,height constraints
inputsContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
inputsContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0).isActive = true
inputsContainerView.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -24).isActive = true
inputsContainerView.heightAnchor.constraint(equalToConstant: 150).isActive = true
Run Code Online (Sandbox Code Playgroud)
那么,为什么我们需要UILayoutGuide对象?事实证明,UIView是否为布局边距属性提供锚属性?请如果有人知道我会非常感激。
ios ×9
autolayout ×5
objective-c ×3
ios6 ×2
swift ×2
storyboard ×1
uiimageview ×1
uitableview ×1
uiview ×1
xcode ×1
xib ×1