尽管阅读了 Apple 文档,例如guide for Windows and Views,但我仍在尝试解决这个问题。 .
此屏幕截图最能说明我的问题:
正如您所看到的,博客文章有一张高度可变的图片(但宽度固定= 320px),它可能会也可能不会与其下面的 View 元素(即标签)重叠。
与使用 CSS 类似,我需要将 UIImageView 以及标签设置为“display:block”。
因此,如果图像占据或多或少的垂直空间,下面的元素将相应地调整其位置。这只能通过编程完成吗?
我尝试通过将每个 View 放置在单独的 View 中来分层地执行此操作,但无济于事。 “剪辑 subview ”未勾选。
非常感谢您提供有关完成此操作的一般程序的建议或相关 Material 的链接。
Controller 代码(postTextLabel和postPicture连接IBOutlet)
#import "DetailViewController.h"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController;
@synthesize scroller;
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
CGRect label_frame = CGRectMake(postTextLabel.frame.origin.x,
postPicture.frame.origin.y + postPicture.frame.size.height,
postTextLabel.frame.size.width,
postTextLabel.frame.size.height);
postTextLabel.frame = label_frame;
}
- (void)configureView
{
if (self.detailItem) {
NSDictionary *post = self.detailItem;
NSString *postText = [post objectForKey:@"post_text"];
NSString *postAuthorName = [post objectForKey:@"post_author_name"];
postTextLabel.numberOfLines = 0;
postTextLabel.text = postText;
postAuthorNameLabel.text = postAuthorName;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *pictureUrl = [post objectForKey:@"post_picture"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:pictureUrl]];
dispatch_async(dispatch_get_main_queue(), ^{
postPicture.image = [UIImage imageWithData:data];
});
});
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
最佳答案
假设您的目标只是让标签捕捉到图像的底部,那么编程是您最好的方法 - 我认为在 Interface Builder 中没有办法做到这一点。如果您将图像和标签分别设置为名为 image
和 label
的 IBOutlet,则如下代码应该能够满足您的目标:
CGRect label_frame = CGRectMake(label.frame.origin.x, image.frame.origin.y + image.frame.size.height, label.frame.size.width, label.frame.size.height);
label.frame = label_frame;
每当图像发生更改时调用此函数,并且文本应位于其下方,标签的上边缘与图像的底部对齐。请记住,如果图像/文本变大,您可能需要更改 ScrollView 的 contentSize
以匹配。
关于iphone - 关于如何设置 Storyboard以使元素不重叠的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12666446/