Ios CGRect大小和返回0的点

Ios CGRect大小和返回0的点,ios,objective-c,cgrect,Ios,Objective C,Cgrect,我试图在scrollview中将这些按钮放置在一行中,但是按钮的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 = @

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

}
//创建一个可变数组来填充
imageNames=[[NSArray alloc]init];
NSMutableArray*mImageNames=[[NSMutableArray alloc]init];
//图像列表
NSString*img1=@“小胡子”;
NSString*img2=@“whitem”;
NSString*img3=@“blackm”;
//将图像添加到可变数组
[mImageNames addObject:img1];
[mImageNames addObject:img2];
[mImageNames addObject:img3];
//将可变数组设置为数组;
图像名称=图像名称;
CGFloat contentSizeWidth=0.0;
CGSize newSize=\u scrollViewOutlet.frame.size;
对于(int i=0;i<[imageNames计数];i++){
NSData*archivedData=[NSKeyedArchivedDataWithRootObject:_firstImage];
UIButton*button=[NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
[按钮添加目标:自我操作:@选择器(按下徽章:)用于控制事件:UIControlEventTouchUpInside];
button.tag=i+1;
CGRect newFrame=[按钮帧];
newFrame.size.width=\u firstImage.frame.size.width;
newFrame.size.height=_firstImage.frame.size.height;
contentSizeWidth=(20+按钮.框架.大小.宽度)*i;
newFrame.origin.x+=(20+\u firstImage.frame.size.width)*i;
newFrame.origin.y=\u firstImage.frame.origin.y;
[按钮设置框:新建框];
NSLog(@“按钮帧大小:%f”,_firstImage.frame.size.width);
NSString*imageName=[[imageName objectAtIndex:i]stringByAppendingString:@“.png”];;
NSString*documentsDirectory=[utils getDocumentsDirectoryPath];
//UIImage*image=[utils loadImage:imageName of Type:@“png”inDirectory:documentsDirectory];
UIImage*image=[UIImage imageName:imageName
];[按钮设置图像:图像状态:uicontrol状态正常];
[按钮设置图像:图像状态:UIControlStateSelected];
[[按钮图像视图]设置内容模式:UIViewContentModeScaleAspectFit];
[按钮设置Alpha:1.0f];
[[按钮图像视图]设置图像:图像];
newSize.width+=10;
newSize.width=contentSizeWidth+100;
[_ScrollViewOutletSetContentSize:newSize];
[_ScrollViewOutletAddSubView:按钮];
}

我认为您需要从头创建一个新的CGRect,然后将其分配给UIView

无法获取现有帧,然后更改其子帧


CGRect newFrame=[按钮帧]//这不是新的框架,而是现有的不可变框架

看起来问题与_firstImage、archivedData和/或button有关。在设置帧之前,您是否检查了这些是否按预期填充?