gpt4 book ai didi

iphone - 如何使用相对点以编程方式定位 View ?

转载 作者:行者123 更新时间:2023-12-03 18:59:01 27 4
gpt4 key购买 nike

当父 View 的边界未知时,相对于其父 View 的大小定位 View 的最佳方法是什么?

如果可能的话,我试图避免硬编码坐标。也许这很愚蠢,如果是这样,那就是一个完全可以接受的答案。

在使用自定义 UI 时,我多次遇到过这种情况。最近的例子是我试图用自定义 View 替换 UINavigationItem 纯文本标题。我希望该 View 填充 super View ,但此外,我希望在右侧有一个 UIActivityIndi​​catorView ,插入约 2 个像素并垂直居中。代码如下:

- (void) viewDidLoad
{
[super viewDidLoad];

customTitleView = [[UIView alloc] initWithFrame:CGRectZero];
customTitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.numberOfLines = 2;
titleLabel.minimumFontSize = 11.0;
titleLabel.font = [UIFont systemFontOfSize:17.0];
titleLabel.adjustsFontSizeToFitWidth = YES;
[customTitleView addSubview:titleLabel];

spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinnerView.center = CGPointMake(customTitleView.bounds.size.width - (spinnerView.bounds.size.width / 2) - 2,
customTitleView.bounds.size.height / 2);
spinnerView.hidesWhenStopped = YES;
[customTitleView addSubview:spinnerView];

self.navigationItem.titleView = customTitleView;
[customTitleView release];
}

这是我的问题:在运行此代码时,customTitleView.bounds 仍然为零。自动调整大小蒙版还没有机会执行其操作,但我非常想要这些值,以便我可以计算其他 subview (此处为事件指示器)的相对位置。

这可能不难看吗?

最佳答案

customTitleView.bounds 宽度和高度为零的唯一原因是您已使用 CGRectZero 以这种方式初始化它。您可以使用任何非零大小初始化 View ,然后根据该任意大小定义其 subview 。只要您正确定义了 subview 的自动调整大小行为,当 super View 的框架在运行时发生变化时,它们的布局就会相应地调整。

例如:

- (void) viewDidLoad
{
[super viewDidLoad];

customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
customTitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

titleLabel = [[UILabel alloc] initWithFrame:customTitleView.bounds];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.numberOfLines = 2;
titleLabel.minimumFontSize = 11.0;
titleLabel.font = [UIFont systemFontOfSize:17.0];
titleLabel.adjustsFontSizeToFitWidth = YES;
[customTitleView addSubview:titleLabel];
[titleLabel release];

spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinnerView.center = CGPointMake(customTitleView.bounds.size.width - (spinnerView.bounds.size.width / 2) - 2,
customTitleView.bounds.size.height / 2);
spinnerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
spinnerView.hidesWhenStopped = YES;
[customTitleView addSubview:spinnerView];
[spinnerView release];

self.navigationItem.titleView = customTitleView;
[customTitleView release];
}

关于iphone - 如何使用相对点以编程方式定位 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2670704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com