我在 in 中创建自定义视图时遇到scrollview
问题iOS
。屏幕上只添加了一张图像,这是最后一张地图,并且只添加了一个标题。其余的不出现。我需要有 4 个图像和 4 个文本。
我的自定义类称为mapview
.
我的自定义视图应如下所示:
这是在ViewController.m中设置滚动视图的方式
-(void)initialiseScreen
{
NSArray *MapGalleryArray = [[Singleton sharedManager] getMapGalleryItems];
int yOffset = 0;
for (int counter=0; counter < [MapGalleryArray count]; counter++)
{
NSString *imageName = [NSString stringWithFormat:@"%@.JPG",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Image"]];
UIImage *MapImage = [UIImage imageNamed:imageName];
NSString *titletext = [NSString stringWithFormat:@"%@",[[MapGalleryArray objectAtIndex:counter] objectForKey:@"Title"]];
MapView *map = [[MapView alloc] initWithImage:MapImage label:titletext];
[MapScrollView addSubview:map];
yOffset +=MapScrollView.frame.size.height/MapGalleryArray count];
}
[MapScrollView setContentSize:CGSizeMake(MapScrollView.frame.size.width, yOffset)];
}
地图视图.h
@interface MapView : UIView
{
UIButton *mapViewButton;
UILabel *titeLabel;
}
@property(nonatomic,retain) UIButton *mapViewButton;
@property(nonatomic,retain) UILabel *titleLabel;
-(id)initWithImage:(UIImage*)image label:(NSString*)text;
地图视图.m
-(id)initWithImage:(UIImage*)image label:(NSString*)text
{
self = [super init];
if (self) {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, image.size.width/2, image.size.height/2);
mapViewButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[mapViewButton setImage:image forState:UIControlStateNormal];
[mapViewButton setImage:image forState:UIControlStateHighlighted];
[mapViewButton addTarget:self action:@selector(ImageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, mapViewButton.frame.size.width+5, mapViewButton.frame.size.height)];
[titleLabel setText:text];
[self addSubview:mapViewButton];
[self addSubview:titleLabel];
}
return self;
}
需要一些指导,我在哪里做错了。抱歉,这是我第一次创建自定义视图。也欢迎任何建议。