以编程方式创建 UIScrollView [英] Creating UIScrollView Programmatically

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

问题描述

我正在尝试以编程方式创建 UIScrollView.我设置一个属性并合成它.

I am trying to create a UIScrollView programatically. I set a property and synthesize it.

@property (weak, nonatomic) IBOutlet UIScrollView *topScrollView;
@synthesize topScrollView;

然后我有一个方法可以做到这一点.

I then have a method that does this.

[topScrollView setFrame:CGRectMake(320, 0, 320, 65)];
[topScrollView setContentSize:CGSizeMake(500, 100)];
[topScrollView setBackgroundColor:[UIColor greenColor]];
[topScrollView setScrollEnabled:YES];
[topScrollView setShowsHorizontalScrollIndicator:YES];
[topScrollView setShowsVerticalScrollIndicator:NO];
[[self view] addSubview:topScrollView]; 

我把它放在 viewDidLoad 中.这不会创建滚动视图.我认为这是因为滚动视图尚未初始化.我可以在上面的方法中进行分配和初始化,但是当我想在另一种方法中使用它时,它就行不通了.我查看了 以编程方式添加的 UIScrollView 在多种方法中使用 但没有太大帮助.可能有一个我不知道的简单解决方案.我该怎么做才能以编程方式创建此滚动视图并能够在另一种方法中使用它(主要是用它来执行动画).

I put this in viewDidLoad. This does not create the scroll view. I think it is because the scroll view has not been initialized. I can do the allocation and initialization in the above method but then when I want to use it in another method it wont work. I looked at Programmatically added UIScrollView used in multiple methods but did not help too much. There is probably a simple solution that I am not aware of. What can I do to programmatically create this scroll view and be able to use it in another method(mainly to conduct animations with it).

谢谢,

伊万

推荐答案

我认为是因为滚动视图尚未初始化.

I think it is because the scroll view has not been initialized.

没错.

我可以在上面的方法中进行分配和初始化,但是当我想在另一种方法中使用它时就行不通了.

I can do the allocation and initialization in the above method but then when I want to use it in another method it wont work.

如果您将新创建的滚动视图分配给属性或实例变量,则会发生这种情况.这就是他们的目的.

It will if you assign your newly minted scroll view to a property or instance variable. That's what they're for.

哦,还有一件事.您需要确保以某种方式保留滚动视图.这意味着将您的 topScrollView 属性更改为 strong,或将其添加到视图(然后将保留它),或两者兼而有之.

Oh, one other thing. You'll need to make sure that your scroll view is retained somehow. That means changing your topScrollView property to strong, or adding it to a view (which will then retain it), or both.

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

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