iphone: partial-screen UIScrollView
I'm a long-time web guy who's pretty new to iphone development. I'm finding many problems I run into stem from thinking about things in terms of web pages, so it's very possible I'm just not asking the right questions on this one. Nonetheless, after a few hours pouring over google results and stackoverflow pages, time to throw this question out into the void:
I'm trying to set up a view that has some static/un-moving stuff at the very top (for example, the title of the content shown below), and allow the rest of the "page" to scroll.
My thought process as it stands now is to create a view that holds the static portion, and a blank space, and then somehow add a scrollview to that blank space, but this is where my knowledge is running dry.
Code-wise, it's looking something like this (i've added comments to show my thought process on using this code):
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);
开发者_JAVA技巧 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];
}
That all works fine and dandy, and pulls in the content correctly, with the minor problem that the scroll view doesn't actually...scroll. If I try to add the ListingDetails view normally, without using this wrapper, the scrolling works just fine, but something I'm doing (or not doing?) above is making the scroll functionality cease to be.
Am I thinking about this all wrong? Ultimate goal is to get a screen where part of it scrolls and part does not, so if it helps to ignore all my rambling above and just address that, go for it.
First off, only one UIViewController per screen. Period. From there you will want two subviews added to UIViewController's main view (the one that's loaded in viewDidLoad). To put this into web terms think of the view controller's view as <body></body>
.
In the end you'll probably want something like this.
- (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];
}
Edit: Setting this up in Interface Builder
In your view controller's .h
@interface SampleViewController : UIViewController
{
UIScrollView *scrollView;
UIView *staticView;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (nonatomic, retain) IBOutlet UIView *staticView;
@end
and in the .m file
@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
Now in the Interface Builder file, you can right-click and drag from File's Owner (that's your UIViewController subclass) directly to the views on screen. When you release a popup will appear and you choose the property in the code you want to connect that view to. Once this is established you can reference those views in code like so.
- (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];
}
I personally wouldn't bother with the scrollView
instance, but do
[self.view addSubview:details.view];
details.view.frame = scrollArea;
But I don't think that's your problem. Another issue with your code is that your view controller ListingDetails
is released at the end of the method, leaving the view without a controller.
Basically, you're going about this wrong. You'd be better off including all your ListingDetails
code in the current view controller, and adding your UIScrollView in Interface Builder. Multiple view controllers can be stacked in a UINavigationController
or a UITabBarController
, but other than that you should only have one UIViewController
.
精彩评论