问题
I know this question is on here a lot, but I still can't seem to get this to work. I'm probably not initiating the view correctly or something... Anyway, I'm trying to add several labels and images to a UIScrollView programmatically. Here is my code for my .h file:
#import <UIKit/UIKit.h>
@interface DOR_HelpViewController : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollView;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@end
And my .m file:
#import "DOR_HelpViewController.h"
@implementation DOR_HelpViewController
@synthesize scrollView;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
scrollView = [[UIScrollView alloc] init];
UILabel *pointsCouponLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 320.0, 15.0)];
pointsCouponLbl.font = [UIFont boldSystemFontOfSize:14.0];
pointsCouponLbl.textAlignment = UITextAlignmentCenter;
pointsCouponLbl.textColor = [UIColor blackColor];
pointsCouponLbl.backgroundColor = [UIColor clearColor];
pointsCouponLbl.text = @"Points Earned Using a Coupon";
[scrollView addSubview:pointsCouponLbl];
UIImageView *pointsCouponImg = [[UIImageView alloc] initWithFrame:CGRectMake(72, 45, 175, 100)];
pointsCouponImg.image = [UIImage imageNamed:@"couponpoints.png"];
[scrollView addSubview:pointsCouponImg];
UILabel *pointsCheckInLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 165.0, 320.0, 15.0)];
pointsCheckInLbl.font = [UIFont boldSystemFontOfSize:14.0];
pointsCheckInLbl.textAlignment = UITextAlignmentCenter;
pointsCheckInLbl.textColor = [UIColor blackColor];
pointsCheckInLbl.backgroundColor = [UIColor clearColor];
pointsCheckInLbl.text = @"Points Earned For Check-In";
[scrollView addSubview:pointsCheckInLbl];
pointsCheckInLbl = nil;
UIImageView *pointsCheckInImg = [[UIImageView alloc] initWithFrame:CGRectMake(72, 190, 175, 100)];
pointsCheckInImg.image = [UIImage imageNamed:@"checkinpoints.png"];
[scrollView addSubview:pointsCheckInImg];
pointsCheckInImg = nil;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
scrollView
is linked to my UIScrollView
object in my storyboard. I would very much appreciate info on what I'm doing wrong, and why if you don't mind. Thanks in advance~
回答1:
Remove scrollView = [[UIScrollView alloc] init];
It's not necessary (even counterproductive) when working with IB.
(actually it was only an idea in a comment - see above. But I try to earn reputation wherever possible ;-) )
回答2:
-(void)addImagesToScrollView:(NSMutableArray*)jsonArray{
if (!jsonArray || jsonArray.count ==0) {
return;
}
int imageXPos=0;
int imageWidth=50;
int imageHeight=50;
for (int index=0; index<[jsonArray count]; index++)
{
NSMutableDictionary *dict=[jsonArray objectAtIndex:index];
NSString *thumbUrl=[JsonUtil getString:dict forKey:@"thumb"];
UIImageView *imageView = [[UIImageView alloc] init ];
[imageView setImageWithURL:[NSURL URLWithString:thumbUrl]
placeholderImage:[UIImage imageNamed:@"place_image"]];
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.clipsToBounds = YES;
imageView.frame = CGRectMake(imageXPos,1, imageWidth, imageHeight);
[self.placeImageScrollView addSubview:imageView];
imageXPos = imageXPos + imageView.frame.size.width +2;//2 is padding
}
self.placeImageScrollView.contentSize = CGSizeMake(imageXPos, self.placeImageScrollView.frame.size.height);
}
来源:https://stackoverflow.com/questions/10434059/adding-uilabels-and-uiimageviews-to-uiscrollview-programmatically