gpt4 book ai didi

ios - CGRect 大小和点返回 0

转载 作者:行者123 更新时间:2023-11-29 03:43:04 25 4
gpt4 key购买 nike

我试图将这些按钮放在 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];

}

最佳答案

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

您无法抓取现有框架然后更改其子框架。

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

关于ios - CGRect 大小和点返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18123173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com