gpt4 book ai didi

ios - 将内容置于 UIScrollView (Pager) iOS 中心

转载 作者:行者123 更新时间:2023-11-28 21:41:30 27 4
gpt4 key购买 nike

我有两个启用了寻呼机的 UIScrollview,我动态添加了 12 个 UIImageView。

enter image description here

我有下一个代码:

int content_size_width = self.view.frame.size.width - arrow_left.frame.size.width - arrow_right.frame.size.width;
int content_size_height = green_scroll.frame.size.height;
first_sign_scroll.frame = CGRectMake(0, 0, content_size_width, content_size_height);
first_sign_scroll.pagingEnabled=YES;
first_sign_scroll.contentSize = CGSizeMake(content_size_width*12,1);
int pos = 0;
for(int i=1;i<13;i++){

UIImage *img = [UIImage imageNamed:images_array[i-1]];
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, img.size.width*multiple, img.size.height*multiple)];
[image setImage: img];

float middle_scroll_x = green_scroll.frame.size.width/2;
float middle_scroll_y = green_scroll.frame.size.height/2;

image.center = CGPointMake(middle_scroll_x + pos,middle_scroll_y);
[green_scroll addSubview:image];
pos +=content_size_width;

}

我为蓝色卷轴做了同样的代码。

问题是图像并不像您在图像中看到的那样位于 ScrollView 的中心。垂直居中不起作用。

有人可以帮我吗?

谢谢。

最佳答案

试试这个方法居中:

@implementation ContentInsetCenteredScrollView

- (void)centerContent {
CGFloat top = 0, left = 0;
if (self.contentSize.width < self.bounds.size.width) {
left = (self.bounds.size.width-self.contentSize.width) * 0.5f;
}
if (self.contentSize.height < self.bounds.size.height) {
top = (self.bounds.size.height-self.contentSize.height) * 0.5f;
}
self.contentInset = UIEdgeInsetsMake(top, left, top, left);
}

- (void)didAddSubview:(UIView *)subview {
[super didAddSubview:subview];
[self centerContent];
}

- (void)scrollViewDidZoom:(__unused UIScrollView *)scrollView {
[self centerContent];
}

- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
[self centerContent];
}

关于ios - 将内容置于 UIScrollView (Pager) iOS 中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31855937/

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