CGRect大小和点回来0

时间:2013-08-08 10:04:03

标签: ios objective-c cgrect

我试图在滚动视图中将这些按钮放在一行,但是按钮的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];

}

1 个答案:

答案 0 :(得分:1)

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

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

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