uiscrollview - 以编程方式将 UILabels 和 UIImageViews 添加到 UIScrollView

标签 uiscrollview uiimageview uilabel addsubview

我知道这个问题在这里经常出现,但我似乎仍然无法让它发挥作用。我可能没有正确启动 View 或其他...无论如何,我正在尝试以编程方式向 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 链接到 Storyboard中的 UIScrollView 对象。如果您不介意的话,我将非常感谢有关我做错了什么以及原因的信息。先谢谢了~

最佳答案

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

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

关于uiscrollview - 以编程方式将 UILabels 和 UIImageViews 添加到 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10434059/

相关文章:

ios - 每当我对嵌入 ScrollView 的内容 View 进行约束时,它就会占用 View Controller 顶部的空间?

iphone - UIScrollView 偶尔会失去弹跳和流畅的滚动

ios - 我如何通过单击按钮来控制标签栏项目?

ios - 为什么在 Web 浏览器中工作的 url 未在 UIImageview 中加载

objective-c - iOS 上的快速模糊

ios - 带有 NSMutableAttributedString 属性文本的 UICollectionViewCell UILabel 更新和显示缓慢

ios - UITableViewCell 中的文本换行不正确

jquery-ui - 使用标记/样式刷新 UI 按钮的 "aria-pressed"

iphone - UIScrollView 重置 contentOffset

ios - 显示 AFNetworking+UIImageView 占位符图像,但不显示 URL 图像