Is it possible to use UIScrollViewController to scroll or "page" two or more views coming from UIViewControllers?
Example (in viewDidLoad)
self.a1 = [[CustomViewController1 alloc] init];
self.a2 = [[CustomViewController2 alloc] init];
//Scroller
self.scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,1000,400
400)];
[self.scroller addSubview:self.a1.view];
[self.scroller addSubview:self.a2.view];
[self.view addSubview:self.scroller];
But I can see in the scroller only the view of controller a2.
You need to set the frames of the views so they they sit one below another. Currently they are both there, just on top of one another.
Try putting in
self.a2.view.frame = CGRectMake(0, 480, 320, 480);
You have to modify the frames aswell. Now a1 and a2 have the same, and they are on top of eachother, so the a2 is visible, because you added the later.
Try
self.a2.view.frame = CGRectOffset(self.a2.view.frame, self.a1.view.frame.size.width, 0);
The solution is to set the frame coordinates of the subviews inside the scroller:
CGRect frame = CGRectMake(0, 0, 1000, 600);
self.a1 = [[CustomViewController1 alloc] init];
self.a1.view.frame = frame;
self.a2 = [[CustomViewController2 alloc] init];
frame.origin.x = frame.size.width +10;
self.a2.view.frame = frame;
[self.scroller addSubview:self.a1.view];
[self.scroller addSubview:self.a2.view];
[self.view addSubview:self.scroller];
You'll need to add the custom view controllers as child view controllers. See Apple's documentation for more information on doing that correctly.
Related
This is what my view controller looks like with these two table views. As you can see, the left looks like the frame is in the intended place and the right does not. I've posted my code and the origin y is in the same place in both. What could be causing this?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGFloat halfLength = self.view.frame.size.width / 2;
CGRect ingredientsFrame = CGRectMake(0, 0, halfLength - 1, self.view.frame.size.height);
CGRect modsFrame = CGRectMake(halfLength + 1, 0, halfLength - 1, self.view.frame.size.height);
_ingredientsTableView = [[UITableView alloc] initWithFrame:ingredientsFrame style:UITableViewStyleGrouped];
_ingredientsTableView.delegate = self;
_ingredientsTableView.dataSource = self;
_ingredientsTableView.tag = 1;
[self.view addSubview:_ingredientsTableView];
_modsTableView = [[UITableView alloc] initWithFrame:modsFrame style:UITableViewStyleGrouped];
_modsTableView.delegate = self;
_modsTableView.dataSource = self;
_modsTableView.tag = 2;
[self.view addSubview:_modsTableView];
}
You need to set the second table view's contentInset and scrollIndicatorInsets to compensate for the fact that the top of the table view is up underneath the navigation bar.
The reason you don't see the same problem in the first table view is that this done for you automatically for the first scroll view in your interface.
Using PEPhotoCropEditor I'm trying to set cropview.croprect but the rect seem to ignore it and still calculate the rect based on the image size. Meanwhile .cropRect works well if I use a controller to access it.
What I'm trying to do is create a fix cropview regardless of what image is selected.
Here's my code:
cropView = [[PECropView alloc] initWithFrame:self.view.bounds];
cropView.image = coverPhotoView.image;
cropView.cropRect = CGRectMake(0, 0, 320, 173);
[self.view addSubview:cropView];
Place your initialisation code into ViewDidLoad:
- (void)viewDidLoad
{
cropView = [[PECropView alloc] initWithFrame:self.view.bounds];
cropView.image = coverPhotoView.image;
[self.view addSubview:cropView];
}
And then set your CropRect in ViewDidAppear:
- (void)viewDidAppear:(BOOL)animated
{
cropView.cropRect = CGRectMake(0, 0, 320, 173);
}
I have a UIViewController and a UIScrollView. Since I have to add multiple views to the view controller, and the scroll view is just one of them, I tried to set the view of the controller to an dummy UIView and the scroll as child, like this:
[self setView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]];
[[self view] setUserInteractionEnabled:NO];
// TDHexMapScrollView inherits from UIViewController
[self setHexMapScrollView:[[TDHexMapScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]];
[[self view] addSubview:[self hexMapScrollView]];
This way the scrolling doesn't work. Adding it as the main view makes scrolling and panning work correctly:
[self setHexMapScrollView:[[TDHexMapScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)]];
[self setView:[self hexMapScrollView]];
Any help? Thanks
Don't do this:
[[self view] setUserInteractionEnabled:NO];
That disables interaction for the view and all subviews. That means it ignores touch events and does not propagate them to its subviews, which means your scrollview doesn't get any events.
I didn't get reply on my comment, but I figured I would show you an example of how I go about doing this.
Declare
CGSize scrollSize;
In your implementation. Then create your scrollView, views, labels, etc.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenFrame.size.width, screenFrame.size.height - self.tabBarController.tabBar.frame.size.height - self.navigationController.navigationBar.frame.size.height)];
self.scrollView.scrollEnabled = YES;
scrollSize.height = self.View1.frame.size.height + self.Label1.frame.size.height + self.Label2.frame.size.height + self.Label3.frame.size.height + self.Label4.frame.size.height + self.Label5.frame.size.height + self.Label6.frame.size.height;
scrollSize.width = screenFrame.size.width;
[self.scrollView setContentSize:scrollSize];
Add to the scrollView
[self.scrollView addSubview:self.View1];
//Repeat for all views / labels / etc
Add the scrollView to the ViewController
[self.view addSubview:self.scrollView];
I'm trying to modify Apple's PhotoScroller example to make the scrollview that is created into a subview instead of it being a view that takes up the entire screen. Any ideas on how this can be accomplished?
- (void)loadView
{
// Step 1: make the outer paging scroll view
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
pagingScrollView.delegate = self;
// When I do this it fails
[self.view addSubview:pagingScrollView];
// Step 2: prepare to tile content
recycledPages = [[NSMutableSet alloc] init];
visiblePages = [[NSMutableSet alloc] init];
[self tilePages];
}
You just need to modify the frame of the scrollview to be positioned and sized how you want:
This is the line in the view controller that sets it up in the example
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
As an example here is a sample frame with some hardcoded values:
CGRect scrollFrame = CGRectMake(100,100,100,100);
pagingScrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
So, I found out that I was able to add the scrollView as a subview by changing the method from loadView to viewDidLoad.
I have no clue why that works, but it does. I'd love to know why that's the case however...
Disclaimer: I'm something of an iOS n00b. I have a navigation-based app -- i.e., in my AppDelegate, I create the nav frame:
self.navigation_controller = [[UINavigationController alloc] initWithRootViewController:home_view_controller];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
self.window.rootViewController = navigation_controller;
self.window.makeKeyAndVisible;
Inside the frame of my home view, I want a search bar, then the scrollable table view. So, I wrote this:
- viewDidLoad{
(HomeView*)home_view = [[HomeView alloc] init];
self.view = home_view
self.view.backgroundColor = [UIColor whiteColor]
(UITableView*)self.table_view = [UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.table_view.bounds.origin.y += 44;
self.table_view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.table_view.dataSource = self;
self.table_view.delegate = self;
[self.view addSubview:self.table_view];
search_frame = self.view.bounds;
search_frame.origin.y = 48.0;
search_frame.size.height = 48.0;
self.search_bar = [[UISearchBar alloc] initWithFrame:search_frame)];
[self.view addSubview:self.search_bar];
}
The table view displays fine, but occupies all the space below the navigation bar. I want to have the navigation bar, then immediately below it the search bar, followed by the table view. At the point of viewDidLoad, the UITableView does not yet have a non-zero bounding rect and I expect that's part of the problem. Additionally, I've specified UIViewAutoresizingFlexibleHeight in my autoresizingMask when I really want it glued against the search bar -- again, I believe this may be part of the problem.
What have I misunderstood here?
Thanks
Steve,
Try following changes:
1) Set tableview's frame using:
CGRect targetRect = CGRectMake(0, 48, self.view.frame.size.width, self.view.frame.size.heigth);
(UITableView*)self.table_view = [UITableView alloc] initWithFrame:targetRect style:UITableViewStylePlain];
2) Add following autosize mask also:
// This value will anchor your table view with top margin
UIViewAutoresizingFlexibleBottomMargin
3) Set searchbar's frame to following rect frame:
CGRecttMake(0, 0, self.view.frame.size.width, 48);
What I would do, is just add the UISearchBar as the first UITableView header.
(method: tableView:viewForHeaderInSection:)
Check this blog post:
http://jainmarket.blogspot.com/2009/08/add-searchbar-as-uitableviews-header.html