Ios 无法在UIScrollView中居中UIImageView
我正在向UIScrollView添加一系列UIImageView,但我失败了,因为内容没有垂直或水平居中 我尝试了以下方法,但似乎不起作用: 在my UiTableViewController viewdidLoad中:Ios 无法在UIScrollView中居中UIImageView,ios,uitableview,uiscrollview,Ios,Uitableview,Uiscrollview,我正在向UIScrollView添加一系列UIImageView,但我失败了,因为内容没有垂直或水平居中 我尝试了以下方法,但似乎不起作用: 在my UiTableViewController viewdidLoad中: UIImage *waitingImage = [UIImage imageWithContentsOfFile:pathOfNoImageFile]; UIImageView *imageview = [[UIImageView al
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
#导入
@界面列表详细信息ScrollView控制器:UIScrollView
@属性(非原子,保留)UIImageView*imageview;
@结束
实施
#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
#导入“ListingDetailScrollViewController.h”
@实现ListingDetailScrollViewController
@综合imageview=\u imageview;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
}
回归自我;
}
/*
//仅覆盖drawRect:如果执行自定义绘图。
//空实现会对动画期间的性能产生不利影响。
-(void)drawRect:(CGRect)rect
{
//绘图代码
}
*/
-(无效)布局子视图{
[超级布局子视图];
//当图像小于屏幕尺寸时,将图像居中
CGSize boundsSize=self.bounds.size;
CGRect frameToCenter=self.imageview.frame;
//水平居中
if(frameToCenter.size.width
有什么建议或想法吗?您可能想看看我的网站上的“使用UIScrollView平移和缩放”教程。在本文中,我解释了如何克服UIScrollView在centering.cRect frameToCenter=self.imageview.frame方面的一些奇怪之处;它不应该是CGRect frameToCenter=self.imageview.bounds;我从中得到了一个似乎很受欢迎的消息answer@tiguero不,因为他想定位iamgeView,他需要修改它的框架,即SuperView(ScrollView)坐标系中的位置和大小,而接收器坐标系中的边界是相同的。