我需要设置translatesAutoresizingMaskIntoConstraints为NO.默认情况下,它设置为YES(以协助大多数从struts和spring转换到新的Auto Layout的应用程序).
Xcode中有哪些地方可以将默认值更改YES为NO?
或者我是否必须为每个视图手动设置它?
我有一个UIButton我在故事板中添加到我的视图控制器的视图.我添加居中约束来定位它并引导空间约束以限制其宽度.在代码中我添加:
self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
[self.button setTitle:@"A real real real real real real real real long long name." forState:UIControlStateNormal];
self.button.backgroundColor = [UIColor redColor];
self.button.titleLabel.backgroundColor = [UIColor blueColor];
Run Code Online (Sandbox Code Playgroud)
结果如下所示:

我希望按钮的大小适合其内容.我怎样才能做到这一点?
我试过了
[self.button sizeToFit];
Run Code Online (Sandbox Code Playgroud)
我已经尝试将内容拥抱和压缩阻力自动布局约束优先级设置为所需.
我也试过明确设置contentEdgeInsets和titleEdgeInsetsto UIEdgeInsetsZero和调用invalidateIntrinsicContentSize.
我还注意到,如果我在标题字符串中放置换行符,那么按钮似乎会调整大小以适应其内容.
我在iPhone 6模拟器上运行Xcode 6和iOS 8.
我试图平滑地制作一个具有自动布局约束的桌面视图.我在我的.h中引用了约束"keyboardHeight",并在IB中将其链接起来.我想要做的就是在弹出窗口时用键盘为表格视图设置动画.这是我的代码:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardFrame = [kbFrame CGRectValue];
CGFloat height = keyboardFrame.size.height;
[UIView animateWithDuration:animationDuration animations:^{
self.keyboardHeight.constant = -height;
[self.view setNeedsLayout];
}];
}
Run Code Online (Sandbox Code Playgroud)
事情是动画块是瞬时的,我看到在键盘完成动画之前出现了空格.所以基本上我看到视图的白色背景,因为键盘是动画.只要键盘是动画,我就无法使动画持续.
我接近这个错误的方式吗?提前致谢!
我想沿着一条线绘制点.

线长取决于设备和方向,并始终在整个屏幕上拉伸.因此,使用相对百分比值来定位点是最有意义的.
到目前为止,我只发现约束在某种点值(Doc)中被定义
是否可以使用百分比值作为约束?关于如何以可扩展的方式定位这些点的任何想法......或者我是否需要"手动"进行这种转换/定位?
我在xcode 5项目的autolayout上遇到了麻烦.我正在使用带有导航控制器的普通视图控制器.我有一个MKMapView在上半部分和一个UITableView在下半部分.我正在使用storyboards,并已配置原型UITableViewCell,但我通过代码添加约束.我已经仔细检查了原型中的每个控件,并且没有看到任何配置.我添加约束时出现问题UITableViewCell.我在单元格中有以下代码:
-(void)updateConstraints {
[super updateConstraints];
//first remove old constraints
[self removeConstraints:self.constraints];
[self.nameLabel removeConstraints:self.nameLabel.constraints];
[self.addressLabel removeConstraints:self.nameLabel.constraints];
[self.rentableSquareFeetLabel removeConstraints:self.rentableSquareFeetLabel.constraints];
[self.lastSaleAmountLabel removeConstraints:self.lastSaleAmountLabel.constraints];
[self.lastSaleDateLabel removeConstraints:self.lastSaleAmountLabel.constraints];
[self.thumbnailImageView removeConstraints:self.thumbnailImageView.constraints];
//then set up constraints
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_thumbnailImageView, _nameLabel, _rentableSquareFeetLabel, _lastSaleAmountLabel, _addressLabel, _lastSaleDateLabel);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_thumbnailImageView(60)]-[_nameLabel(<=200)]-(>=8)-[_rentableSquareFeetLabel]-(>=8)-[_lastSaleAmountLabel]|" options:0 metrics:nil views:viewsDictionary]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_nameLabel]-(-4)-[_addressLabel]" options:NSLayoutFormatAlignAllLeading metrics:nil views:viewsDictionary]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_lastSaleAmountLabel]-(-4)-[_lastSaleDateLabel]" options:NSLayoutFormatAlignAllLeading metrics:nil views:viewsDictionary]];
}
Run Code Online (Sandbox Code Playgroud)
我在调试控制台中收到以下内容.该异常由第一个addConstraints行触发.如果我只是继续通过那些,那么最终一切都显示应该是,因为看起来xcode正在选择打破正确的约束:
2013-09-25 15:07:14.169 PECProperties[32381:a0b] Unable to simultaneously satisfy constraints. Probably at least …Run Code Online (Sandbox Code Playgroud) 我一直在试图用在雨燕自动布局视觉格式语言,使用NSLayoutConstraint.constraintsWithVisualFormat.这是一些没有用的代码的例子,但据我所知,应该让类型检查器开心:
let foo:[AnyObject]! = NSLayoutConstraint.constraintsWithVisualFormat(
format: "", options: 0, metrics: {}, views: {})
Run Code Online (Sandbox Code Playgroud)
但是,这会触发编译器错误:
"无法转换表达式的类型'[AnyObject]!' 输入'String!'".
在我认为这是一个值得雷达的bug之前,我有什么明显的遗漏吗?即使没有显式转换变量名,也没有使用其他无偿的向下转换,这种情况就会发生as.我看不出任何理由为什么编译器会期望将其任何部分解析为a String!.
如何使用UIViewController容器转换方法使用自动布局:
-(void)transitionFromViewController:(UIViewController *)fromViewController
toViewController:(UIViewController *)toViewController
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL finished))completion;
Run Code Online (Sandbox Code Playgroud)
传统上,使用Springs/Struts,您可以设置初始帧(在调用此方法之前),并在传递给方法的动画块中设置最终帧.
该方法可以将视图添加到视图层次结构并为您运行动画.
问题是您不能在同一位置(在方法调用之前)添加初始约束,因为视图尚未添加到视图层次结构中.
任何想法我如何使用此方法以及自动布局?
以下是使用Springs/Struts(框架)执行此操作的示例(谢谢cocoanetics) http://www.cocoanetics.com/2012/04/containing-viewcontrollers
- (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
// XXX We can't add constraints here because the view is not yet in the view hierarchy
// animation setup
toViewController.view.frame = _containerView.bounds;
toViewController.view.autoresizingMask = _containerView.autoresizingMask;
// notify
[fromViewController willMoveToParentViewController:nil];
[self addChildViewController:toViewController];
// transition
[self transitionFromViewController:fromViewController
toViewController:toViewController
duration:1.0
options:UIViewAnimationOptionTransitionCurlDown
animations:^{
}
completion:^(BOOL finished) {
[toViewController didMoveToParentViewController:self];
[fromViewController removeFromParentViewController];
}];
}
Run Code Online (Sandbox Code Playgroud) 我想把我的文字放在一个UILabel,但对于不同的iPhone,其大小UILabel正在改变,因为我正在使用自动布局,但我无法修复字体大小,所以我的文字正在删除.
有什么方法可以设置任何约束,以便文本适应UILabel动态?
我已经阅读了文档.但是我还不确定什么时候不需要设置它false.在下面的代码中,如果我将其设置为falseI,则根本不会看到标题.如果我把它留下来true,那么一切都会好的.
在View调试层次结构中,它将发出警告" 宽度和位置不明确".
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()
header.translatesAutoresizingMaskIntoConstraints = false
header.backgroundColor = .orange
header.heightAnchor.constraint(equalToConstant: 10).isActive = true
return header
}
Run Code Online (Sandbox Code Playgroud)
我想,每当我需要修改代码中的任何东西,我将不得不设置translatesAutoresizingMaskIntoConstraints到false.
也许更正确的说法是你需要删除所有约束然后将其设置为false然后添加你喜欢的东西,在这种情况下你需要为所有4个边添加约束.
但是,如果您需要保留系统提供给您的内容,在这种情况下,tableView将管理其位置和宽度,然后离开true.
是对的吗?
autolayout ×10
ios ×9
objective-c ×3
swift ×2
uitableview ×2
xcode ×2
ios6 ×1
uibutton ×1
uikeyboard ×1
uilabel ×1
uiscrollview ×1
uiview ×1
xcode4.5 ×1
zoom ×1