- objective-c - iOS 5 : Can you override UIAppearance customisations in specific classes?
- iphone - 如何将 CGFontRef 转换为 UIFont?
- ios - 以编程方式关闭标记的信息窗口 google maps iOS
- ios - Xcode 5 - 尝试验证存档时出现 "No application records were found"
好吧,另一个 UITableViewCell 动态高度问题,但有点扭曲。不幸的是,我不能只在发布时跳转到 iOS 8,否则问题就解决了。需要 iOS >= 7.1。
我正在尝试实现一个单元格,在单元格顶部有两个图像,在它们下方有一个标题标签,在其下方有一个描述标签。我知道顶部的两个图像是正方形的,因此我希望它们具有相同的大小并保持正方形的纵横比,但在屏幕尺寸变化时调整大小(例如方向改变或不同的设备)。
可能有助于可视化的图像(由于代表不能包含。没关系颜色,可视化帮助): http://i.stack.imgur.com/kOhkl.png
注意最后一个文本之后的空隙,那不应该在那里。
我在以前的应用程序中根据以下内容实现了动态高度: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights (成功),现在也使用相同的方法。
我已经使用 Storyboard和编程方式设置了约束,但没有成功。
不过,这是关键。调用时:
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
在:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
高度值远大于阅读时的高度:
cell.contentView.frame
另一个问题是,如果我删除图像的纵横比限制并仅将其更改为明确的高度限制,它就可以正常工作。
对于任何愿意提供帮助的人,我整理了一个非常简单的示例项目来说明问题: https://github.com/alefr/DynamicTableCellHeight
它被设置为使用 Storyboard作为约束,并且有一个额外的分支:ExplicitHeightConstraint,它除了将宽高比约束更改为图像的高度约束外什么都不做。
**所以问题是:**任何人都可以看到使高度计算混淆的约束有什么问题,或者是否有人有任何替代建议来获得想要的结果? (虽然我想为 View 使用自动布局)。
作品中有很多限制(虽然没有什么特别的)所以我认为查看提供的 Storyboard (github 链接)比在此处明确说明更容易。但是,如果有人认为我也可以这样做。估计的高度计算如下:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Don't care about memory leaks now:
DynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dynamicCell"];
[self populateCell:cell withContent:self.tableData[indexPath.row]];
// Make sure the constraints have been set up for this cell, since it may have just been created from scratch.
// Use the following lines, assuming you are setting up constraints from within the cell's updateConstraints method:
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
// Set the width of the cell to match the width of the table view. This is important so that we'll get the
// correct cell height for different table view widths if the cell's height depends on its width (due to
// multi-line UILabels word wrapping, etc). We don't need to do this above in -[tableView:cellForRowAtIndexPath]
// because it happens automatically when the cell is used in the table view.
// Also note, the final width of the cell may not be the width of the table view in some cases, for example when a
// section index is displayed along the right side of the table view. You must account for the reduced cell width.
cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds));
// Do the layout pass on the cell, which will calculate the frames for all the views based on the constraints.
// (Note that you must set the preferredMaxLayoutWidth on multi-line UILabels inside the -[layoutSubviews] method
// of the UITableViewCell subclass, or do it manually at this point before the below 2 lines!)
[cell setNeedsLayout];
[cell layoutIfNeeded];
// Get the actual height required for the cell's contentView
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
// So we can read debug values
CGRect contentFrame = cell.contentView.frame;
CGRect firstImageFrame = cell.firstArtwork.frame;
CGRect secondImageFrame = cell.secondArtwork.frame;
CGRect nameFrame = cell.name.frame;
CGRect numArtworksFrame = cell.numArtworks.frame;
// Add an extra point to the height to account for the cell separator, which is added between the bottom
// of the cell's contentView and the bottom of the table view cell.
height += 1.0f;
return height;
}
- (void)populateCell:(DynamicTableViewCell*)cell withContent:(DynamicContent*)content
{
cell.firstArtwork.image = content.firstImage;
cell.secondArtwork.image = content.secondImage;
cell.name.text = content.name;
cell.numArtworks.text = [NSString stringWithFormat:@"%@ artworks", content.numImages];
}
谢谢
最佳答案
我遇到了同样的情况,并解决了它以覆盖给定单元格中的 systemLayoutSizeFittingSize
函数并将 aspectRation 约束替换为高度约束。在下面的示例中,mediaPlayerAspectRationConstraint
在 View 生命周期中被添加到 View 中,mediaPlayerHeightContraint
将用于确定单元格的正确高度(参见下面的代码) .
所以,我用了
CGFloat height = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
而不是 [cell.contentView systemLayoutSizeFittingSize:]
。
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize {
self.mediaPlayerHeightContraint.constant = CGRectGetHeight(self.experienceMedia.frame);
//Lets replace it with an exact height constraint (workaround).
//To remove the Unable to simultaneously satisfy constraints. exceptions
[self.contentView layoutIfNeeded];
[self.experienceMedia removeConstraint:self.mediaPlayerAspectRationConstraint];
[self.experienceMedia addConstraint:self.mediaPlayerHeightContraint];
[self setNeedsUpdateConstraints];
CGSize res = [self.contentView systemLayoutSizeFittingSize:targetSize];
[self.contentView layoutIfNeeded];
[self.experienceMedia removeConstraint:self.mediaPlayerHeightContraint];
[self.experienceMedia addConstraint:self.mediaPlayerAspectRationConstraint];
[self setNeedsUpdateConstraints];
return res;
}
关于ios - UIView纵横比混淆systemLayoutSizeFittingSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25664716/
我有一个 uiview,其中包含其他自定义绘制的 uiview。现在我希望,当用户单击此父 View 时,它将扩展到带有动画的新定义的框架。我使用动画 block 取得了一些成功。问题是展开的 Vie
我正在创建当前显示的 UIView 的屏幕截图。在此过程中,我遇到了控件扭曲和拉伸(stretch)片刻(0.2-0.5 秒)的问题。它仅适用于 iPhone 6、6+。 我的简单代码在这里 (Xam
我想让主 UIView 负责将多个 UIView 之一添加为 subview 。由于任何时候只有一个 subview 处于事件状态,而 subview 完全覆盖了主视图,所以我还不如更换主视图。但这需
这真的只是好奇,但是标签属性有最大值吗?我假设标签是一个 UINT,但我不确定。 最佳答案 “标签”属性是一个 NSInteger。来自 Apple 的 docs : NSInteger Used t
我的布局中有一个 UIView 以便进行一些剪辑和分组,但是自动布局会在缩小时调整它的大小。我想给它一个固定的高度,但唯一的选择是设置顶部和底部空间。 有没有办法设置明确的高度约束? 最佳答案 我刚刚
使用 NSLayoutConstraint类,可以创建基于 View 基线 ( NSLayoutAttributeBaseline ) 的约束。但是,我还没有看到任何描述 UIView 的文档。实际上
我正在尝试创建一个 UIView,它显示一个半透明的圆圈,其边界内有一个不透明的边框。我希望能够通过两种方式更改边界 - 在 -[UIView animateWithDuration:animatio
我目前正在 iOS 中开发一个项目(使用 XCode 和 Swift)。我正在尝试为登录 View 实现以下 UITextFields: 我正在考虑不同的方法来做这件事,但它们看起来都很复杂。如果有人
我正在使用最新的 SDK 开发 iOS 应用程序。 我创建了一个自定义 UIView,我需要向该 UIView 添加一些控件。我也在使用 storyboards,并且我必须向其中添加这个 UIView
我已经通过界面生成器在 super View 中启用了Autoresize Subviews。有没有办法以编程方式禁用一个 subview View 的自动调整大小? 最佳答案 我假设您确实在使用自动
我正在寻找用 Swift 2.0 编写的适用于 iOS 8.0 或更高版本的答案。我有一个圆形的 UIView,我想扩展它但保留圆形的属性。 要设置 UIView,这是代码。 let frame: C
我有一个有趣的问题,但我不确定如何解决它。我正在尝试使用 @IBInspectable 扩展 UIView。但是,使用这种方法,拐角半径似乎是从 nib 文件的默认端设置的,而不是 View 的实际大
我想将 UIView 移动到圆圈内。 UIView 移动圆内的每个点,但不接触圆的边界线。我正在计算圆和 UIView 之间的距离。 var distance = sqrt( pow((tou
A 是一个自定义的 UIView B是UIImagePickerController.view,这个UIImagePickerController.showsCameraControls = NO A
我无法在启动时修改我的 UIView 高度。 我必须使用 UIView,我希望其中一个屏幕大小 * 70,另一个填补空白。 这是我的 @IBOutlet weak var questionFrame
我一直在想这个问题。 我的情况是我需要在 View 中显示很多网格,我有两个计划。 1、在UIView上画很多矩形。我试过了,但是当我放大 View 时,这条线很模糊。 2、给UIView添加很多CA
我可以通过简单地将容器拖动到新的 UIView 来成功地将第一个 UIView 嵌入到容器中,但是要添加过渡 View ,我无法在按键输入事件上用新 View 替换当前的嵌入 View 。 这是我的代
问题是... 我有一条路径,我创建了一个在其中绘制它的 View 。 View 与路径具有相同的维度。然后,我创建第二个 View ,在其中重写 sizeThatFit: 方法,以便缩放第一个 Vie
我正致力于快速设计自定义 UIimageview。我想使用类似于此的 beizerpath 创建一个 UIimageview 编码应该是快速的。任何帮助表示赞赏。谢谢 最佳答案 创建一个 CAShap
我有一个 iOS 应用程序可以在 UIView 上动态绘制形状(通过 drawRect),我正在研究将该应用程序(或其中的一小部分)移植到 Apple Watch 的可能性。不幸的是,在阅读相关帖子后
我是一名优秀的程序员,十分优秀!