我在滚动视图中添加了一些按钮。我正在用图像设置按钮的背景。当您看到输出时,图像似乎被拉伸了。我想避免这种情况。
所以我试过这个:
创建了一个自定义视图,它处理图像并使该图像远离边缘固定。
为自动调整大小部分做了这个:
我的代码如下所示:
for (int counter = 0; counter < imageCount; counter++) {
NSString *imageName = [NSString stringWithFormat:@"%@_img0%d.jpg",mRoutePointId,counter+1];
UIImage *infoImage = [UIImage imageNamed:imageName];
GalleryImage *galleryItem = [[GalleryImage alloc] initWithImage:infoImage];
UIButton *infoImageButton = [[UIButton alloc] initWithFrame:CGRectMake(xOffset, 0, infoImagesScrollView.frame.size.width, infoImagesScrollView.frame.size.height)];
infoImageButton.tag = counter;
[infoImageButton setImage:galleryItem.galleryImage forState:UIControlStateNormal];
[infoImageButton setImage:galleryItem.galleryImage forState:UIControlStateHighlighted];
[infoImageButton addTarget:self action:@selector(infoImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[infoImageButton setTitle:[NSString stringWithFormat:@"%@_img0%d.jpg",mRoutePointId,counter+1] forState:UIControlStateApplication];
[infoImagesScrollView addSubview:infoImageButton];
xOffset+=infoImagesScrollView.frame.size.width;
}
自定义视图.m
#import "GalleryImage.h"
@implementation GalleryImage
@synthesize galleryImage;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithImage:(UIImage*) image
{
self = [super init];
if (self) {
// galleryImageView = nil;
galleryImage = image;
}
return self;
}
-(void)awakeFromNib
{
[galleryImageView setImage:galleryImage];
}
需要一些有关如何确保图像在添加到按钮时不会拉伸的指导。
也欢迎其他建议..