使UIView在全宽水平UIScrollView中粘到顶部

时间:2016-06-17 10:07:56

标签: ios objective-c iphone uiscrollview

我有UIScrollView,其中包含全宽图像。我想在每个时间点UIView之上UIScrollView。最初输出是正确的,我在UIView上获得UIScrollView。但是当我向右滚动时,UIView正在移动,第一个图像向左移动。如何让UIView始终保持最佳状态?我是否需要实施任何其他方法?

-(void)setSalonImagesScrollView{

int x = 0;

for (int i = 0; i < [self.imageList count]; i++) {

    CGFloat xOrigin = i * self.view.frame.size.width;
    UIImageView *salImg =[[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,0,320,194)];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageList[i]]];

    salImg.image = [UIImage imageWithData:imageData];
    salImg.tag = i;
    [self.salonImgCarousel addSubview:salImg];
    [self.salonImgCarousel insertSubview:self.navSubView belowSubview:self.navSubView];

    x += salImg.frame.size.width;
}

self.salonImgCarousel.contentSize = CGSizeMake(x, self.salonImgCarousel.frame.size.height);

[_salonImgCarousel setShowsHorizontalScrollIndicator:NO];
[_salonImgCarousel setDelegate:self];}

3 个答案:

答案 0 :(得分:0)

问题在于,您将imageView添加到ScrollView。您将添加到ScrollView的所有内容都将滚动。您必须将imageView添加到View而不是ScrollView。

[self.view addSubview:salImg];

答案 1 :(得分:0)

试试这个

-(void)setSalonImagesScrollView{
int x = 0;

for (int i = 0; i < [self.imageList count]; i++) {

CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView *salImg =[[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,0,320,194)];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.imageList[i]]];

salImg.image = [UIImage imageWithData:imageData];
salImg.tag = i;
[self.view addSubview:salImg];
[self.salonImgCarousel insertSubview:self.navSubView belowSubview:self.navSubView];

x += salImg.frame.size.width;
}

self.salonImgCarousel.contentSize = CGSizeMake(x, self.salonImgCarousel.frame.size.height);

[_salonImgCarousel setShowsHorizontalScrollIndicator:NO];
[_salonImgCarousel setDelegate:self];}

并发布您的难度

亲切的问候

科希克

答案 2 :(得分:0)

如果您希望每次都在屏幕上UIView,那么最好的办法是在UIView上添加UIWindow

实施例

[window addSubView:myView];

另请参阅How can access window in my view controller