0

我的滚动视图会反弹,但不会滚动。

这是我的代码片段:

- (void)populateStampList {

    // create a mutable array to populate

    imageNames = [[NSArray alloc] init];
    NSMutableArray *mImageNames = [[NSMutableArray alloc] init];


    // List of images

    NSString *img1 = @"moustache";
    NSString *img2 = @"whitem";
    NSString *img3 = @"blackm";
    NSString *img4 = @"monacle";
    NSString *img5 = @"tophat";


    // add images to mutable array

    [mImageNames addObject:img1];
    [mImageNames addObject:img2];
    [mImageNames addObject:img3];
    [mImageNames addObject:img4];
    [mImageNames addObject:img5];


    // set mutable array to array;

    imageNames = mImageNames;
    CGFloat contentSizeWidth = 0.0;
    CGSize newSize = _scrollViewOutlet.frame.size;


    for(int i = 0; i < [imageNames count]; i++){

        NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:_firstImage];
        UIButton *button = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
        [button addTarget:self action:@selector(badgePressed:) forControlEvents:UIControlEventTouchUpInside];

        button.tag = i+1;

        CGRect newFrame = CGRectMake(_firstImage.frame.origin.x, _firstImage.frame.origin.y, _firstImage.frame.size.width, _firstImage.frame.size.height);
        newFrame.size.width = _firstImage.frame.size.width;
        newFrame.size.height = _firstImage.frame.size.height;
        newFrame.origin.x += (20+_firstImage.frame.size.width) * i;
        newFrame.origin.y =  _firstImage.frame.origin.y ;
        [button setFrame:newFrame];
         contentSizeWidth = (20+button.frame.size.width) * i;

        NSLog(@"button frame size: %f", _firstImage.frame.size.width);

        NSString *imageName = [[imageNames objectAtIndex:i] stringByAppendingString:@".png"];;

        NSString *documentsDirectory = [utils getDocumentsDirectoryPath];

        //UIImage *image = [utils loadImage:imageName ofType:@"png" inDirectory:documentsDirectory];
        UIImage *image = [UIImage imageNamed:imageName
                          ];        [button setImage:image forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateSelected];
        [[button imageView] setContentMode: UIViewContentModeScaleAspectFit];
        [button setAlpha:1.0f];
        [[button imageView] setImage:image];

        [_scrollViewOutlet addSubview:button];
        NSLog(@"scrollViewFrame size: %f", _scrollViewOutlet.frame.size.width);
        NSLog(@"scrollViewFrame size: %f", _scrollViewOutlet.frame.size.height);

        newSize.width = contentSizeWidth+400;
        [_scrollViewOutlet setScrollEnabled:YES];
        [_scrollViewOutlet setContentSize:newSize];
        NSLog(@"scrollviewOutlet Content Width: %f", _scrollViewOutlet.contentSize.width);
        NSLog(@"scrollviewOutlet Content Height: %f", _scrollViewOutlet.contentSize.height);


    }


}

这是我的 NSLogs:

2013-08-08 18:28:39.856 xxxx[4647:907] scrollViewFrame size: 320.000000
2013-08-08 18:28:39.857 xxxx[4647:907] scrollViewFrame size: 86.000000
2013-08-08 18:28:39.858 xxxx[4647:907] scrollviewOutlet Content Width: 768.000000
2013-08-08 18:28:39.859 xxxx[4647:907] scrollviewOutlet Content Height: 86.000000
4

1 回答 1

0

我解决了这个问题。这是一个需要在视图中取消勾选的自动布局复选框。

于 2013-08-09T14:35:12.720 回答