0

サブビューを画像として持つスクロールビューがあります.スクロールビューの中央に写真を配置すると、それらはすべてスタックとして重なり合って表示されます.水平にスクロールする必要があります..どうすれば修正できますか? これはこれまでの私のコードです..

- (void)viewDidLoad
{

    [super viewDidLoad];

    for (int i = 1; i < 18; i++) {
        UIImageView *images = [[UIImageView alloc] initWithImage:[UIImage imageNamed: [NSString stringWithFormat:@"%d.jpg", i]]];

       images.frame = CGRectMake((i-1)*320, 0, 320, 330);

        [scroller setContentOffset:CGPointMake(0, 0)];
        [scroller addSubview:images];
        [images setContentMode:UIViewContentModeScaleAspectFill];
        [images sizeToFit];

        //center image
        CGSize boundsSize = scroller.bounds.size;
        CGRect frameToCenter = images.frame;

        // center horizontally
        if (frameToCenter.size.width < boundsSize.width) {
            frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
            NSLog(@"%f", frameToCenter.origin.x);
        }
        else
            frameToCenter.origin.x = 0;

        // center vertically
        if (frameToCenter.size.height < boundsSize.height)
            frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
        else
            frameToCenter.origin.y = 0;

        images.frame = frameToCenter;

        scroller.pagingEnabled = NO;


    }


    [scroller setContentMode:UIViewContentModeScaleAspectFit];

    scroller.delegate =self;
    scroller.contentSize = CGSizeMake(320*17, 330);
    scroller.backgroundColor = [UIColor blackColor];
    pageControl.numberOfPages =17;
    pageControl.currentPage = 0;
4

1 に答える 1

0

画像を水平方向に中央に配置するには、以下のコードを使用してください

float scrollHeight = 0;    
    for (int col = 0; col < index; ++col)
        {
            CGRect selectFrame = CGRectMake(0,0,kOvrW,kOvrH);
            selectionIndicator = [[UIImageView alloc] initWithFrame:selectFrame];
            selectionIndicator.contentMode = UIViewContentModeBottomRight;
            [selectionIndicator setImage:[UIImage imageNamed:@"checked"]];
            CGRect frame = CGRectMake(5,kOvrPreY+col*(kOvrPreH+kOvrH),kOvrW,kOvrH);
            UIView *fr = [[UIView alloc] initWithFrame:frame];

            CGRect imgFrame = CGRectMake(5, 4, 68, 55);
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:imgFrame];
            imgView.image = [UIImage imageNamed:[[subCategories objectAtIndex:col] valueForKey:@"add"]];

            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
            tapGesture.numberOfTapsRequired = 1;
            tapGesture.numberOfTouchesRequired = 1;
            [fr setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"small-thumb"]]];
            [fr addGestureRecognizer:tapGesture]; 
            [fr addSubview:imgView];
            fr.tag = col;
            [subCatScroll addSubview:fr];

            [subCatScroll bringSubviewToFront:fr];
            scrollHeight += kOvrPreH+kOvrH;
        }
    subCatScroll.contentSize = CGSizeMake(subCatScroll.frame.size.width, scrollHeight+kOvrPreH);
于 2012-07-26T12:17:21.443 に答える