以编程方式将 UILabels 和 UIImageViews 添加到 UIScrollView [英] Adding UILabels and UIImageViews to UIScrollView programmatically

查看:34
本文介绍了以编程方式将 UILabels 和 UIImageViews 添加到 UIScrollView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题经常出现,但我似乎仍然无法解决这个问题.我可能没有正确启动视图或其他东西......无论如何,我正在尝试以编程方式向 UIScrollView 添加几个标签和图像.这是我的 .h 文件的代码:

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

还有我的 .m 文件:

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

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~

推荐答案

Remove scrollView = [[UIScrollView alloc] init]; 使用 IB 时没有必要(甚至适得其反).

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 ;-) )

这篇关于以编程方式将 UILabels 和 UIImageViews 添加到 UIScrollView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆