gpt4 book ai didi

ios - 保持 UIImageView 和 UILabel 居中对齐

转载 作者:行者123 更新时间:2023-11-29 10:40:31 28 4
gpt4 key购买 nike

我有一个 UIImageView 和一个 UILabel 通过 XIB 并排放置在屏幕的中心,现在标签文本可以是小的/大的,它是动态的,图像是恒定的。

当标签文本较小时,图像和标签看起来居中对齐,但当标签文本较大时,图像仍居中对齐,但现在标签尾部几乎触及屏幕。

这是图片:

enter image description here

希望你已经明白了,我怎样才能让这两个(图像和标签)始终居中对齐。

最佳答案

- (void)alignImageView:(UIImageView *)imageView andLabel:(UILabel *)label inSuperView:(UIView *)superView {
[label sizeToFit]; // Resize the label to have a frame that perfectly fit the content
/* We first set the X origin of the imageView,
* To center anything on the X axis, the common way is to say that x is "(superview.width - subview.width) / 2"
* Here subview width is imageView.width + label.width
*/
[imageView setFrame:CGRectMake((superView.frame.size.width - imageView.frame.size.width - label.frame.size.width) / 2, imageView.frame.origin.y, imageView.frame.size.width, imageView.frame.size.height)];
/* According to the previous calculation, we just have to say that the x origin for the label is just at the end of the imageView (origin + size)
*/
[label setFrame:CGRectMake(imageView.frame.origin.x + imageView.frame.size.width, label.frame.origin.y, label.frame.size.width, label.frame.size.height)];
}

类似的东西应该可以工作。

关于ios - 保持 UIImageView 和 UILabel 居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24775389/

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