0

我试图在滚动视图中将这些按钮连续放置,但按钮的 CGRect 的 x 和 y 位置不会填充,并且宽度、高度和 x 和 y 位置的每个变量似乎总是打印 0。

// 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";


// add images to mutable array

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


// 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 = [button frame];
    newFrame.size.width = _firstImage.frame.size.width;
    newFrame.size.height = _firstImage.frame.size.height;
    contentSizeWidth = (20+button.frame.size.width) * i;
    newFrame.origin.x += (20+_firstImage.frame.size.width) * i;
    newFrame.origin.y =  _firstImage.frame.origin.y ;
    [button setFrame:newFrame];


    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];


    newSize.width += 10;
    newSize.width = contentSizeWidth+100;

    [_scrollViewOutlet setContentSize:newSize];
    [_scrollViewOutlet addSubview:button];

}
4

1 回答 1

1

我相信您需要从头开始创建一个新的 CGRect,然后将其分配给 UIView。

你不能抓住现有的框架,然后改变它的孩子。

CGRect newFrame = [按钮框架]; //这不是一个新框架,它是现有的不可变框架

于 2013-08-08T10:19:43.693 回答