Tag: 自动布局

只有在显示之后,setNeedsLayout才会重新传递单元格

我有一个单元格的表视图,有时有一个可选的UI元素,有时它必须被删除。 根据元素,标签被resize。 当单元格被初始化时,它会比稍后更窄。 当我将数据设置到标签中时,将从cellForRowAtIndexPath:调用此代码cellForRowAtIndexPath: if (someFlag) { // This causes layout to be invalidated [flagIcon removeFromSuperview]; [cell setNeedsLayout]; } 之后,单元格返回到表格视图,并显示。 但是,该点的文字标签已经调整了其宽度,而不是高度。 一秒钟左右高度就会被调整,当所有的细胞已经被显示时,这个混蛋清晰可见。 重要提示,这只是在最初几个单元格的初始创build期间。 一旦它们被重用,一切都很好,因为可选视图被删除,并且标签已经正确地形成了以前的用法。 为什么在setNeedsLayout之后但在显示之前没有完全重新布局单元格? 显示之前不应该UIKit检查无效的布局? 如果我做 if (someFlag) { [flagIcon removeFromSuperview]; [cell layoutIfNeeded]; } 所有得到调整一次,但它似乎是一个不正确的方式来编写代码,我觉得我失去了别的东西。 有关如何创build单元格的更多代码: – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ProfileCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; [cell setData:model.items[indexPath.row] forMyself:YES]; return cell; } […]

自动布局UILabels

我有三个UILabels在我的自定义UITableViewCell 。 这可能是一些UILabels将是空的( label.text == @"" ) – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"EventCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; NSString *key = [[keysArray objectAtIndex:indexPath.section] description]; UILabel *nameLabel = (UILabel *)[cell viewWithTag:100]; namelabel.text = @"Label 1"; UILabel *locationLabel = (UILabel *)[cell viewWithTag:101]; location.text = @"Label 2"; UILabel *timeLabel = (UILabel *)[cell […]

使用自动布局约束将子类UIView添加到Nib

我正在尝试创build一个包含2个自定义视图(B)的UIView(A) 视图B使用Autolay Const Constraints进行设置,并在Interface Builder中进行设置,包括约束。 A被添加到viewController的Nib中 B – UIImageView(Leading = 10,Trailing = 10,AlignVertically) – UITextField(Leading = 10,Trailing = 10,AlignVertically) ViewController A(300×300,水平alignment,垂直alignment) 在ViewController中,我将A固定为300×300,B1和B2将其前导,尾随,顶部和底部固定为0.(这应该使B1和B2成为300×150,如果我错过了某些东西,请原谅我) 当加载View BI时,使用下面的代码来加载它的Nib: override func awakeAfterUsingCoder(aDecoder: NSCoder!) -> AnyObject! { if self.subviews.count == 0 { let bundle = NSBundle(forClass: self.dynamicType) var view = bundle.loadNibNamed("B", owner: nil, options: nil)[0] as B view.setTranslatesAutoresizingMaskIntoConstraints(false) let constraints = […]

为什么AutoLayout没有照顾我的UICollectionView

我已经使用UICollectionView构build了一个非常简单的应用程序示例( github上的源代码)。 一切工作正常,只要该应用程序在肖像模式。 然而,当它改变为横向模式时,内容单元没有适当地resize,因此什么都不显示。 我认为所有必要的AutoLayout约束都已到位。 我知道我可以实现collectionView:layout:sizeForItemAtIndexPath:但我的目标是尽可能多地使用AutoLayout(只是为了更好地理解AutoLayout)。 我在这里错过了什么?

iOS7 XIB问题。 顶部和底部的空白空间

我正在设置一个ImageView的视图。 自动布局被选中。 它在预览中看起来不错,但是当应用程序实际在模拟器上运行时,仅在iPHone视网膜4英寸模拟器的顶部和底部才会显示空白区域。 在3.5英寸看起来不错。 使用iOS7和XCODE 5。 即使我从视图中删除图像,仍然有同样的问题。 什么可以是这个空白的理由?

在约束依赖于框架的自定义视图中使用自动布局

我正在编写一个自定义视图,以编程方式初始化。 我重写updateConstraints以添加此视图所需的所有约束。 : – (void)updateConstraints { [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]]; [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; [self.superview addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; // some more constraints, you get the point self.bottomSpacingConstraint = [NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:-(0.2 * CGRectGetHeight(self.bounds))]; [self addConstraint:self.bottomSpacingConstraint]; […]

如何定位相对于包含视图大小的子视图?

我希望能够将我的子视图放置在超级视图大小的25%的顶部。 NSLayoutConstraint *topPositionConstraint = [NSLayoutConstraint constraintWithItem:_containerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_childView attribute:NSLayoutAttributeHeight multiplier:0.25f constant:0.0f]; 但是,现在我收到以下exception: 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Invalid pairing of layout attributes' 为什么错误发生,我怎么能达到我想要的?

iOS 7/8 UITableView Cell:两个具有dynamic高度的UILabel,以及可变行高的自动布局

所以当我只有一个根据string长度改变高度的标签时,我可以用自动布局设置dynamic高度尺寸。 我的问题是,如果我添加另一个应该这样做的UILabel,事情不会奏效。 我将内容拥抱优先级和压缩阻力设置为1000这两个==我得到不明确的警告 如果我将第二个UILabel的内容拥抱(垂直)设置为999或250,那么它的效果很好,但是只有第二个标签有两个或更多的线。 如果第二个标签为空或仅有一行,则heightForRowAtIndexPath systemLayoutSizeFittingSize:UILayoutFittingCompressedSize height会返回较大的值,并且这些单元格具有较大的空格。 我也玩过内在大小:默认或占位符(有几个高度和宽度),但它也没有帮助。 任何人有什么build议可以做什么?

iOS:带有1个标签和3个button的简单AutoLayout

我正在玩AutoLayout,真的把我的头撞在墙上,看起来应该是一个非常简单的解决scheme。 我有一个垂直的控件列:1个标签和3个button。 我希望标签的高度是40像素(点),并根据其超视图的宽度(左侧,顶部和右侧的标准间距)自动调整宽度。 我想要3个button在该标签下方垂直排列。 我想他们的宽度自动resize就像标签。 我想他们的间距是标准的(aqua?)间距(8分,对吧?)。 我想3个button是相同的高度。 我可以得到我想要的工作,但是我一直在运行时在控制台中出现错误,我想弄清楚为什么我要得到它们,以及如何避免得到它们。 我已经观看了AutoLayout上的WWDCvideo,以下是我到目前为止所尝试的: UILabel *label = [self Label]; MMGlossyButton *button1 = … MMGlossyButton *button2 = … MMGlossyButton *button3 = … [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button1]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(new)]]; [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(existing)]]; [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button3]-|" […]

UITableViewCell + Autolayout + RTL

我将我的应用程序本地化为希伯来语,这是一种RTL语言。 每个单元格具有水平约束设置为前导/尾随在RTL语言自动反转。 最后的结果与预期完全一致,但是当滚动浏览表格视图时,UITableViewCell子视图从LTR animation到RTL,这非常奇怪。 有没有办法来防止animation的意见? 例: 在所有的WWDCvideo中也没有这个词