【问题标题】:Adding UILabels and UIImageViews to UIScrollView programmatically以编程方式将 UILabel 和 UIImageViews 添加到 UIScrollView
【发布时间】:2012-05-03 15:00:54
【问题描述】:

我知道这个问题在这里很多,但我似乎仍然无法让它发挥作用。我可能没有正确启动视图或其他东西......无论如何,我正在尝试以编程方式将几个标签和图像添加到 UIScrollView 。这是我的 .h 文件的代码:

#import <UIKit/UIKit.h>

@interface DOR_HelpViewController : UIViewController <UIScrollViewDelegate> {
    IBOutlet UIScrollView *scrollView;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;

@end

还有我的 .m 文件:

#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 链接到我故事板中的UIScrollView 对象。我非常感谢有关我做错了什么的信息,以及如果您不介意的原因。先谢谢了~

【问题讨论】:

  • 我从不使用 IB/故事板,但您确定必须分配/初始化 IB 中定义的视图吗?即scrollView = [[UIScrollView alloc] init];是必要的吗?
  • 不,我不确定。正如我应该尝试的那样,因为这是我正在考虑的事情之一。你能把它作为答案吗?我刚刚删除了它,它起作用了。

标签: uiscrollview uiimageview uilabel addsubview


【解决方案1】:

删除scrollView = [[UIScrollView alloc] init]; 与 IB 合作时没有必要(甚至适得其反)。

(实际上这只是评论中的一个想法 - 见上文。但我会尽可能地赢得声誉;-))

【讨论】:

    【解决方案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);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-04-03
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 2012-02-29
      • 1970-01-01
      相关资源
      最近更新 更多