1

我正在向 UIScrollView 添加一系列 UIImageViews 并且我失败了,因为东西没有垂直或水平居中。

我尝试了以下方法,但它似乎不起作用:

在我的 UiTableViewController viewdidLoad 中:

UIImage *waitingImage = [UIImage imageWithContentsOfFile:pathOfNoImageFile];
                     UIImageView *imageview = [[UIImageView alloc] init];

              [imageview  setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:waitingImage];

                     [imageview setContentMode:UIViewContentModeScaleAspectFit];
                     self.scrollview.imageview = imageview;
                     [self.scrollview addSubview:imageview];

我正在使用自定义 UIScrollview 以便它可以进行居中:

标题

#import <UIKit/UIKit.h>
@interface ListingDetailScrollViewController : UIScrollView
@property (nonatomic, retain) UIImageView *imageview;
@end

执行

#import "ListingDetailScrollViewController.h"

@implementation ListingDetailScrollViewController
@synthesize imageview=_imageview;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void)layoutSubviews {
    [super layoutSubviews];

    // center the image as it becomes smaller than the size of the screen
    CGSize boundsSize = self.bounds.size;
    CGRect frameToCenter = self.imageview.frame;

    // center horizontally
    if (frameToCenter.size.width < boundsSize.width)
        frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
    else
        frameToCenter.origin.x = 0;

    // center vertically
    if (frameToCenter.size.height < boundsSize.height)
        frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
    else
        frameToCenter.origin.y = 0;

    self.imageview.frame = frameToCenter;
}

@end

任何提示或想法?

4

0 回答 0