在界面构建器中,我们可以固定高度,引脚宽度,使两个视图宽度相等,但是如何设置约束,以便在调整视图大小时,它保持宽度/高度比?
在我的特定情况下,我的视图控制器中有一个UIImageView.当视图调整大小时,我希望我的图像视图调整大小,但保持3:2的宽度:高度比.在IB中可以做到吗?是否可以使用代码执行此操作?
谢谢!
我有一个自定义的容器控制器,就像UITabBarController,但有切换动画.我使用自动布局来实现.
因为子控制器之间的切换是动态的,所以当将其视图添加到容器的视图中时,将适当的约束添加到子视图控制器,而不是在IB中预先设置.(当然,约束被添加到超级视图中)
http://d.pr/i/q6NF容器控制器笔尖设置
PS:约束的细节
H:| [Child](将约束的常数从左/右改为左/右)
H:[Child(== Super)]
V:| [Child] |
其中一个子控制器是导航控制器,导航控制器出现模态视图控制器(使用presentViewController:animated:completion:)时出现问题,并解除它(使用dismissViewControllerAnimated:completion:),当前/解除导航控制器的框架变为(x,y,0, 0),似乎自动布局变得无效,可能已删除约束.
http://d.pr/i/VmvL现在/解雇程序
我还没有使用代码来验证这些约束发生了什么,但是使用Spark Inspector,我看到在当前/解除过程中视图表示发生了变化.当我的导航控制器呈现模态视图控制器时,iOS只是将整个导航控制器的视图交换到模态视图控制器的视图.当导航控制器的视图返回时,自动布局不再起作用.
我想出的解决方案之一是让我的容器控制器提供模态控制器.
或者我只是将容器控制器更改为无自动布局.
事实上,自从我开始使用自动布局以来,这种技术带来的问题只会占据优势.除了这个问题,每次界面方向改变时,我的容器控制器内的视图都无法自动布局,似乎子视图在方向改变之前总是使用超视图的框架.我仔细检查我设置的约束,没有冲突,没有歧义.
我的猜测是我的自定义容器控制器与自动布局系统中的模态视图控制器和界面方向更改不兼容,即使设置了约束.
Xcode 5 beta,iOS 7SDK,目标iOS6.1也许SDK环境不对劲?
如果Foursquare签入成功,我的iPhone应用程序会在显示的视图顶部添加一个视图.
我希望视图以X和Y为中心,并且使用autolayout使其具有明确的宽度和高度,但我不确定以编程方式添加哪些约束.我知道如何在Storyboard中执行此操作,但我不确定要在代码中输入什么内容才能执行相同操作,因为稍后会添加此视图以响应应用程序中的成功操作.
我无法正常工作,UIView有一个用loadNibNamed加载的nib:
SuccessView *successfulCheckInView = [[SuccessView alloc] initWithFrame:CGRectZero];
successfulCheckInView.placeNameLabel.text = properVenue.name;
successfulCheckInView.placeAddressLabel.text = properVenue.address;
successfulCheckInView.delegate = self;
[self.ownerVC.view addSubview:successfulCheckInView];
Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个UITableView,其行为与twitter客户端的时间轴类似.现在我只是想在UITableViewCell中获得两个标签.根据Stack Overflow的回答,我为每个布局使用了不同的reuseIdentifier.我的布局很简单,由单个标签或两个标签组成.最终我将调整UITableViewCells的高度,但首先我需要填充内容的单元格.
如果我设置框架initWithFrame:,我可以显示标签,但是约束没有实现.
问题:什么阻止标签和约束出现?我在UITableViewCell的实现中显然遗漏了一些东西,但我不知道它是什么.
次要问题:我是否为每个reuseIdentifier正确注册了UITableViewCell类viewDidLoad?
这可能会遇到困难,但Interface Builder会让我感到困惑,我想在代码中实现这一点.
以下是名为TVTCell.h的自定义UITableViewCell的代码:
static NSString * const kCellIDTitle = @"CellWithTitle";
static NSString * const kCellIDTitleMain = @"CellWithTitleMain";
@interface TVTCell : UITableViewCell
{
NSString *reuseID;
}
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *mainLabel;
@end
Run Code Online (Sandbox Code Playgroud)
和TVTCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
reuseID = reuseIdentifier;
nameLabel = [[UILabel alloc] init];
[nameLabel setTextColor:[UIColor blackColor]];
[nameLabel setBackgroundColor:[UIColor colorWithHue:32 saturation:100 brightness:63 alpha:1]];
[nameLabel setFont:[UIFont …Run Code Online (Sandbox Code Playgroud) 我正在实现一个与UINavigationController非常相似的自定义容器,除了它不包含整个控制器堆栈.它有一个UINavigationBar,它被约束到容器控制器的topLayoutGuide,它恰好是顶部的20px,这没关系.
当我添加一个子视图控制器并将其视图放入层次结构时,我希望在IB中看到它的topLayoutGuide并用于布置子视图控制器的视图子视图以显示在导航栏的底部.需要注意的是相关文档中要做的事情:
具体而言,此属性的值是查询此属性时返回的对象的length属性的值.此值受视图控制器或其封闭容器视图控制器(如导航或标签栏控制器)的约束,如下所示:
- 不在容器视图控制器内的视图控制器将此属性限制为指示状态栏的底部(如果可见),
或者指示视图控制器视图的上边缘.- 容器视图控制器中的视图控制器不会设置此属性的值.相反,容器视图控制器约束值以指示:
- 如果导航栏可见,则导航栏的底部
- 如果只显示状态栏,则状态栏的底部
- 如果状态栏和导航栏都不可见,则视图控制器视图的上边缘
但我不太明白如何"约束它的价值"因为topLayoutGuide和它的长度属性都是只读的.
我试过这个代码来添加一个子视图控制器:
[self addChildViewController:gamePhaseController];
UIView *gamePhaseControllerView = gamePhaseController.view;
gamePhaseControllerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentContainer addSubview:gamePhaseControllerView];
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[gamePhaseControllerView]-0-|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(gamePhaseControllerView)];
NSLayoutConstraint *topLayoutGuideConstraint = [NSLayoutConstraint constraintWithItem:gamePhaseController.topLayoutGuide
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.navigationBar
attribute:NSLayoutAttributeBottom
multiplier:1 constant:0];
NSLayoutConstraint *bottomLayoutGuideConstraint = [NSLayoutConstraint constraintWithItem:gamePhaseController.bottomLayoutGuide
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.bottomLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1 constant:0];
[self.view addConstraint:topLayoutGuideConstraint];
[self.view addConstraint:bottomLayoutGuideConstraint];
[self.contentContainer addConstraints:horizontalConstraints];
[gamePhaseController didMoveToParentViewController:self];
_contentController = gamePhaseController;
Run Code Online (Sandbox Code Playgroud)
在IB中,我为gamePhaseController指定"Under Top Bars"和"Under Bottom Bars".其中一个视图特别限制在顶部布局指南中,无论如何在设备上它看起来是容器导航栏底部20px ...
使用此行为实现自定义容器控制器的正确方法是什么?
我创建了一个看起来像这样的视图控制器:

我希望两个顶部按钮在它们自己和整个视图的左/右边缘之间总是有20个点.它们也应始终具有相同的宽度.我已经为所有这些创建了约束,它完全符合我的要求.问题是垂直约束.按钮应始终位于顶部边缘下方20个点.它们应该具有相同的高度.但是,autolayout不会认为左侧标签需要两行来容纳其所有文本,因此结果如下所示:

我希望它看起来像在第一张照片中.我无法为按钮添加恒定的高度限制,因为当应用程序在iPad上运行时,只需要一行,那么拥有额外的空间将是浪费.
在viewDidLoad我试过这个:
- (void)viewDidLoad
{
[super viewDidLoad];
self.leftButton.titleLabel.preferredMaxLayoutWidth = (self.view.frame.size.width - 20.0 * 3) / 2.0;
self.rightButton.titleLabel.preferredMaxLayoutWidth = (self.view.frame.size.width - 20.0 * 3) / 2.0;
}
Run Code Online (Sandbox Code Playgroud)
但这并没有改变任何事情.
问题:如何使自动布局尊重左按钮需要两行?
首先,我有3个不同的UIViews来替换iPad故事板上的Split View Controller中的细节视图
它在iOS8 iPad上运行良好.但是当我加载其中一个细节视图时,应用程序在iOS7和iOS 6 Simulator中运行时崩溃.
我只假设这是因为我的故事板上的自动布局.
有谁知道如何修理它?
2014-09-25 04:15:19.705 PSTappsperance[48327:60b] Pad AppDelegate ########
2014-09-25 04:15:27.869 PSTappsperance[48327:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to create description in descriptionForLayoutAttribute_layoutItem_coefficient. Something is nil'
*** First throw call stack:
(
0 CoreFoundation 0x0000000110a5c495 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001107af99e objc_exception_throw + 43
2 CoreFoundation 0x0000000110a5c2ad +[NSException raise:format:] + 205
3 Foundation 0x00000001104ec548 descriptionForLayoutAttribute_layoutItem_coefficient + 145
4 Foundation 0x00000001104ec3bc -[NSLayoutConstraint equationDescription] + 216
5 Foundation 0x00000001104ec831 -[NSLayoutConstraint description] + …Run Code Online (Sandbox Code Playgroud) 我想禁用自动添加Relative to margin创建约束时IB的Xcode 6.
很高兴他们默认启用此选项,但我必须支持iOS7,所以事实证明我每次添加新约束后都必须手动禁用此选项.
我发现此设置可以预先禁用它,但默认情况下它始终处于启用状态.

我正在使用Storyboard开发一个Swift项目,我希望文本包含在标签中.在我没有使用故事板的旧Objective-C版本中,我使用了以下设置,并且工作完美.

以下是Swift的设置

我一直在阅读有关首选宽度设置的潜在自动布局问题.我目前将它们设置为自动布局,标签本身设置为宽度560.我添加了一个约束,使标签与尾随的超级视图保持20像素,虽然我认为这样可行但我仍然无法获取文本包裹.尺寸设置如下.

有人可以解释如何包装文本吗?
我有一个只包含UILabel的视图.此标签包含多行文字.父级具有可变宽度,可以使用平移手势调整大小.我的问题是,当我这样做时,调整UILabel的大小不会重新计算其高度,使得所有内容仍然可见,它只是将其切断.
我已经设法通过一些黑客来修复它但运行速度非常慢:
- (void)layoutSubviews {
CGSize labelSize = [self.labelDescription sizeThatFits:CGSizeMake(self.frame.size.width, FLT_MAX)];
if (self.constraintHeight) {
[self removeConstraint:self.constraintHeight];
}
self.constraintHeight = [NSLayoutConstraint constraintWithItem:self.labelDescription attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:labelSize.height];
[self addConstraint:self.constraintHeight];
[super layoutSubviews];
}
Run Code Online (Sandbox Code Playgroud)
这不应该自动布局自动发生吗?
编辑
我的观点结构是:
UIScrollView
---> UIView
---> UILabel
Run Code Online (Sandbox Code Playgroud)
以下是UIScrollView的约束:
<NSLayoutConstraint:0x120c4860 H:|-(>=32)-[DescriptionView:0x85c81c0] (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c48a0 H:|-(32@900)-[DescriptionView:0x85c81c0] priority:900 (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c48e0 H:[DescriptionView:0x85c81c0(<=576)]>,
<NSLayoutConstraint:0x120c4920 H:[DescriptionView:0x85c81c0]-(>=32)-| (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x120c4960 H:[DescriptionView:0x85c81c0]-(32@900)-| priority:900 (Names: '|':UIScrollView:0x85db650 )>,
<NSLayoutConstraint:0x8301450 DescriptionView:0x85c81c0.centerX == UIScrollView:0x85db650.centerX>,
Run Code Online (Sandbox Code Playgroud)
以下是UIView的约束:
<NSLayoutConstraint:0x85c4580 V:|-(0)-[UILabel:0x85bc7b0] (Names: '|':DescriptionView:0x85c81c0 )>,
<NSLayoutConstraint:0x85c45c0 H:|-(0)-[UILabel:0x85bc7b0] (Names: '|':DescriptionView:0x85c81c0 )>, …Run Code Online (Sandbox Code Playgroud) autolayout ×10
ios ×9
objective-c ×3
ios7 ×2
xcode ×2
constraints ×1
ios6 ×1
iphone ×1
storyboard ×1
swift ×1
uibutton ×1
uilabel ×1
uitableview ×1
uiview ×1
xcode6 ×1