iphone:部分屏幕UIScrollView
我是一个很长时间的网络人物,他对iphone开发很新颖。我发现很多问题是因为网页方面的问题而产生的,所以很可能我只是没有在这个问题上提出正确的问题。尽管如此,经过几个小时浇筑谷歌的结果和*页面,时间把这个问题扔出去:iphone:部分屏幕UIScrollView
我想建立一个视图,在最顶部有一些静态/不移动的东西(例如,下面显示的内容的标题),并允许“页面”的其余部分滚动。
现在我的思维过程是创建一个视图,它保存静态部分和空白空间,然后以某种方式将scrollview添加到该空白空间,但这是我的知识枯竭的地方。
代码明智的,它看起来是这样的(我已经添加评论显示在使用此代码我的思维过程):
DetailsWrapper.m:
-(void)viewDidLoad {
//initialize details view (custom constructor that populates some stuff)
//ListingDetails is a subclass of UIViewController whose view is set to be a UIScrollView created in IB
//the viewDidLoad method of ListingDetails sets the contentSize of the scroll view
ListingDetails *details = [[ListingDetails alloc] initWithNibName:@"ListingDetails" andListingData:theListing];
//create an empty view that takes up all but the top 45px of the iphone screen
CGRect scrollArea = CGRectMake(0.0f, 45.0f, 320.0f, 435.0f);
UIView *scrollView = [[UIView alloc] initWithFrame:scrollArea];
//add the UIScrollView to the space we just made
[scrollView addSubview:details.view];
//now show it
[self.view addSubview:scrollView];
//cleanup
[scrollView release];
[details release];
}
这一切都工作正常,丹迪,并正确地拉内容,小问题,滚动视图实际上不滚动...滚动。如果我试图正常地添加ListingDetails视图,而不使用这个包装器,滚动工作得很好,但是我正在做的(或不做的)是让滚动功能停止。
我在想这个全错吗?最终目标是获得一个屏幕,其中一部分滚动,一部分不滚动,因此,如果忽略上述所有散乱的内容并解决这个问题,那就去做吧。
首先,每个屏幕只有一个UIViewController。期。从那里你会想要两个子视图添加到UIViewController的主视图(在viewDidLoad中加载的视图)。把它放到网络术语中,把视图控制器的视图看作为<body></body>
。
最后你可能会想要这样的东西。
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 45.0)];
// Further configure your scroll view as you presumably already have in ListingDetails
[self.view addSubview:scrollView];
[scrollView release];
UIView *staticView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 45.0, 320.0, 435.0)];
[self.view addSubview:staticView];
[staticView release];
}
编辑:最多在Interface Builder
现在
@implementation SampleViewController
@synthesize scrollView;
@synthesize staticView;
- (void)viewDidUnload
{
[super viewDidUnload];
self.scrollView = nil;
self.staticView = nil;
}
- (void)dealloc
{
[scrollView release], self.scrollView = nil;
[staticView release], self.staticView = nil;
[super dealloc];
}
@end
在Interface Builder中设置这个在您的视图控制器的.h
@interface SampleViewController : UIViewController
{
UIScrollView *scrollView;
UIView *staticView;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIView *staticView;
@end
,并在.m文件文件,您可以右键单击并从File's Owner(这是您的UIViewController子类)直接拖到视图 在屏幕上。当你释放一个弹出窗口将会出现,你在代码中选择你想要连接该视图的属性。一旦建立,您可以像这样在代码中引用这些视图。
- (void)viewDidLoad
{
[super viewDidLoad];
// Assume we need to make some tweaks once the view has loaded
self.staticView.backgroundColor = [UIColor redColor];
self.scrollView.backgroundColor = [UIColor blackColor];
}
我个人不会与scrollView
实例麻烦,但确实
[self.view addSubview:details.view];
details.view.frame = scrollArea;
但我不认为这是你的问题。您的代码的另一个问题是您的视图控制器ListingDetails
在该方法结束时被释放,留下没有控制器的视图。
基本上,你正在讨论这个错误。你最好在你的当前视图控制器中包含所有ListingDetails
代码,并在Interface Builder中添加你的UIScrollView。多个视图控制器可以堆叠在UINavigationController
或UITabBarController
中,但除此之外,您应该只有一个UIViewController
。
啊......你让* scrollView成为一个UIScrollView本身,而不是只有一个作为它的子视图。这似乎是诀窍。现在看看我是否可以设法停止堆叠ViewControllers在另一个之上... – 2011-02-22 20:27:13