iphone - 关于如何设置 Storyboard以使元素不重叠的建议

标签 iphone objective-c ios ios5 uiimage

尽管阅读了 Apple 文档,例如guide for Windows and Views,但我仍在尝试解决这个问题。 .

此屏幕截图最能说明我的问题:

enter image description here

正如您所看到的,博客文章有一张高度可变的图片(但宽度固定= 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

enter image description here

最佳答案

假设您的目标只是让标签捕捉到图像的底部,那么编程是您最好的方法 - 我认为在 Interface Builder 中没有办法做到这一点。如果您将图像和标签分别设置为名为 imagelabel 的 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/

相关文章:

iphone - 检测 2 个 subview 的触摸

objective-c - 是否可以在运行时更改 Storyboard语言?

ios - 将 NSMutableAttributedString 转换为 NSString

ios - UITableView 滚动到内容大小内的部分

iOS 动画 : CADisplayLink vs CAShapeLayer

javascript - JSON 应用程序未响应网络代码

ios - 在 iOS 的不同设备上为同一个项目使用不同的文件?

iphone - 移动端和网络服务器之间的数据同步

iphone - 我在 Cocoa 的 SOAP XML 中使用什么数据类型作为整数?

iphone - 从 Mac App 获取 iPhone 信息的方法