Ios 使用PSCollectionView将UIImageView居中

Ios 使用PSCollectionView将UIImageView居中,ios,objective-c,uiimageview,Ios,Objective C,Uiimageview,在我的应用程序中,我使用的是,我想在其中显示文本标签和图像。我对如何使图像和文本居中越来越着迷。我试着这样做: - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 75)]; self

在我的应用程序中,我使用的是,我想在其中显示文本标签和图像。我对如何使图像和文本居中越来越着迷。我试着这样做:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100, 75)];
        self.labelName = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 100, 20)];
        self.labelName.font = [self.labelName.font fontWithSize:12];
        self.labelName.textAlignment = NSTextAlignmentCenter;

        [self addSubview:self.productImage];
        [self addSubview:self.labelName];

        self.backgroundColor = [UIColor colorWithRed:236.0f/255.0f green:236.0f/255.0f blue:236.0f/255.0f alpha:1.0];
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = 1.0f;
        self.layer.cornerRadius = 10.0f;
        self.layer.borderColor= [[UIColor colorWithRed:207.0f/255.0f green:207.0f/255.0f blue:207.0f/255.0f alpha:1] CGColor];
    }
    return self;
}
但结果是:

你可以看到文本和图像不在中心,我如何才能使它们居中?注意:图像可能有不同的大小。 你能帮我找个方法纠正这个观点吗

更新: 以下是
cellforrow方法
和下载图像的方法:

- (PSCollectionViewCell *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index {
    ProductViewCell *cell = (ProductViewCell *)[self.psView dequeueReusableViewForClass:nil];
    if (!cell) {
        cell = [[ProductViewCell alloc]initWithFrame:CGRectMake(10, 70, 100, 100)];
    }
    cell.labelName.text = [[self.arrayWithData objectAtIndex:index]objectForKey:@"name"];
    NSURL * url = [NSURL URLWithString:[[self.arrayWithData objectAtIndex:index]objectForKey:@"url"]];

    [self loadImageFromWeb:url andImageView:cell.productImage];
    return cell;
}

- (void) loadImageFromWeb:(NSURL *)urlImg andImageView:(UIImageView *)imageView {
    //NSURLRequest* request = [NSURLRequest requestWithURL:url];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:urlImg];

    NSString *authCredentials =@"user:password";
    NSString *authValue = [NSString stringWithFormat:@"Basic %@",[authCredentials base64EncodedStringWithWrapWidth:0]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];

    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse * response,
                                               NSData * data,
                                               NSError * error) {
                               if (!error){
                                   UIImage *image = [[UIImage alloc] initWithData:data];
                                   [imageView setImage:image];
                               } else {
                                   NSLog(@"ERRORE: %@", error);
                               }

                           }];
}

您使用的是框架,因此我假设您从代码(而不是故事板)开始执行所有操作,因此:

这里有不同的
x
值。这可能会导致您的it出现问题

您应该做的是读取单元格框,然后使用它设置内容框。所以您应该从方法调用它:

- (MyCell *)collectionView:(PSCollectionView *)collectionView cellForRowAtIndex:(NSInteger)index
{
    MyCell *cell = (MyCell *)[collectionView dequeueReusableViewForClass:[MyCell class]];
    if (cell == nil) {
        cell = [[MyCell alloc] initWithFrame:CGRectMake(0,0,collectionView.frame.size.width,50)];
    }
   // here you have to do other stuff, like assigning display text or something like that.
}
这将创建高度为
50
的单行单元格。如果您想要两行,您应该实现计数器并使用“x”值进行操作

我真的不知道
PSCollectionView
。你为什么需要它?
UICollectionView
相当不错

编辑:

太好了,所以您只需在cell类的
initWithFrame
中更改以下行: 浮动wMargin=5

self.productImage = [[UIImageView alloc]initWithFrame:CGRectMake(wMargin, 5, frame.size.width-(wMargin*2), 75)];
self.labelName = [[UILabel alloc]initWithFrame:CGRectMake(wMargin, 80, frame.size.width-(wMargin*2), 20)];
如果要从边缘移动内容,应使用
wMargin
变量进行操作

EDIT2: 嗯,奇怪。尝试在
cellforrowatinex

cell = [[ProductViewCell alloc]initWithFrame:CGRectMake(10, 70, 100, 100)];
致:


你使用自动布局吗?这是一个自定义视图,我不确定它是否使用自动布局。。。有没有办法知道我是否在使用自动布局?有一种方法可以通过使用不带故事板/xib的代码来居中图像?您的收藏视图有xib吗?没有,我正在按代码设计视图我使用该视图,因为我想要一个类似于pinterest的视图。几天前我问过如何进行“墙视图”,有人建议我使用PSCollection视图。我将使用
UICollectionView
和此自定义布局。但是如果你已经从这个开始,那么最好还是坚持下去。你可以在问题中粘贴
initWithFrame
。来自哪个类?
initWithFrame
位于扩展
PSCollectionViewCell
的类上。我必须把标签放在单元格的底部边界上,文本居中,图像应该在这个单元格和centerOK中,很好。因此,您应该从
cellforrowatinex
传递一个帧。请在你的问题中展示这个方法。它不会。。。我这样做了:我创建了一个变量
float wMargin=5.0
,然后我将您的代码复制到我的类中,但是当我在模拟器上尝试应用程序时,图像和标签没有居中
cell = [[ProductViewCell alloc]initWithFrame:CGRectMake(10, 70, 100, 100)];
cell = [[ProductViewCell alloc] initWithFrame:CGRectMake(0,0,collectionView.frame.size.width/2,100)];