Can't get a scroll view to work - ios

I've been trying all day to get a very simple screen scroll working in Xcode 5 and just can't do it.
Even this simple tutorial doesn't work for me http://www.youtube.com/watch?v=m5Knw41Tz_M. All that does is add a scroll view to the view controller and when run, it shows a blank screen that can scroll - seen by the bars at the side. No matter what I do, I can't get this to work. First time I've needed scrolling and I am obviously missing something incredibly simple.
Simple .h code:
#interface ViewController : UIViewController {
IBOutlet UIScrollView *Scroller;
}
The .m code is
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[Scroller setScrollEnabled:YES];
[Scroller setContentSize:CGSizeMake(320, 1000)];
}
#end
The Scroller is connected to the scroll view, and that is about it! I've tried adding in a UIView to the UIScrollview, but nothing seems to work.

Your code needs to move to viewdidAppear or viewWillAppear
Try changing your method to this
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[Scroller setScrollEnabled:YES];
[Scroller setContentSize:CGSizeMake(320, 1000)];
}

This is wired, I followed your code and something happened to reset scroll view content size as CGSizeZero if I add subview into UIScrollView in IB. If I add subview by code, the scroll view would be properly. I haven't figure out reason yet.
But one thing you can work around is that you can set scroll view content size in
- (void) viewDidAppear:(BOOL)animated.
Or add subview by code in ViewDidLoad.

i am not good at english, i just will show code what is work..
Simple .h code:
#interface ViewController : UIViewController {
IBOutlet UIScrollView *Scroller;
}
The .m code is
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:#selector(something) userInfo:nil repeats:YES];
}
- (void)something {
//[Scroller setScrollEnabled:YES]; !!!This don't need..
[Scroller setContentSize:CGSizeMake(320, 1000)];
}
#end

Related

Toolbar is not hidden after segue

I have trouble w/ hiding toolbar when leave screen by segue.
Details: App has a few dozen screens, all of them are belonged the same navigation controller. A few screens have toolbar. For these a few screens in -(void)viewDidLoad I use
[self.navigationController setToolbarHidden:NO animated:NO];
and in -(void)viewWillDisappear:(BOOL)animated:
[self.navigationController setToolbarHidden:YES animated:YES];
so toolbar is visible only on necessary screens and the screen which needs toolbar controls the visibility. All work well when I navigate by "back" button.
The trouble starting when I try to navigate by segue like this (goto home & goto another branch).
[owner.navigationController popToRootViewControllerAnimated:NO];
[self performSegueWithIdentifier:SEGUE_NAME sender:self];
toolbar is stay visible in spite of calling -(void)viewWillDisappear which should hide toolbar.
are there any ideas how to perform these "ToolBarHidden" by right way.
thanks
PS: of course I could hide toolbar on every screen, but I want to avoid this unnecessary operations and want to know how to do it right.
**STEP1:** in your controller.h
#interface ViewController : UIViewController {
UIToolbar *bar;
}
#property (nonatomic, strong) IBOutlet UIToolbar *bar;
#end
**STEP2:** in your controller.m
#synthesize bar;
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
self.bar.hidden = NO;
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
self.bar.hidden = YES;
}
**STEP3:** connect in Intrface
hope this help you!

Does UIScrollView only work on a view controller that is embedded in a Navigation Control?

Does UIScrollView only work on a view controller that is embedded in a Navigation Control? The reason I ask is because I have a UIViewController that has a UISCrollView to show all the content that exceeds beyond the view. This ViewController is displayed from a push segue from a TableViewController, which is embedded into a Navigation Controller.
Everything worked fine until I tried to change the push segue to a modal seque instead, which thus removed the view from being embedded into a Navigation Controller.
All the content loads just fine but it won't scroll. I have added an outlet and tried programmatically setting the contentSize and scrollEnabled properties but still nothing. It won't scroll any more and I can't figure it out. I have been searching on the net but I can't find an answer.
Outlet:
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
Implementation:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = CGSizeMake(320, 900);
self.scrollView.scrollEnabled = YES;
}
There are a number of posts indicating issues with setting the contentSize of a scrollView in viewDidLoad. Try moving it to viewDidAppear.
I had to put the UIScrollView in another empty UIView in order to get it to work.
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated
{
self.scrollView.contentSize = CGSizeMake(320, 900);
self.scrollView.scrollEnabled = YES;
}

UIViewController containment

I'm trying to use the UIViewController containment feature in one of my projects. The app is meant to be used in landscape mode only.
I add UIViewController A as a child of UIViewController B and add A's main view as a subview of one of B's views. I'm also saving a reference to A in B:
#interface BViewController : UIViewController
#property (retain, nonatomic) AViewController *aVC;
#end
#implementation BViewController : UIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:#"A"];
[self addChildViewController:self.aVC];
[self.myContainerView addSubview:self.aVC.view];
}
#end
The problem I have is that landscape orientation is not being respected. I did some debugging and found a solution, but I fear is not ideal as it's more of a hack:
In B:
- (void)viewDidLoad
{
[super viewDidLoad];
self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:#"A"];
[self addChildViewController:self.aVC];
[self.myContainerView addSubview:self.aVC.view];
[self.aVC didMoveToParentViewController:self];
}
In A:
- (void)didMoveToParentViewController:(UIViewController *)parentVC
{
// Interchange width and height
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.**height**, self.view.frame.size.**width**);
}
Am I missing something here?
Your code:
self.aVC = [self.storyBoard instantiateViewControllerWithIdentifier:#"A"];
[self addChildViewController:self.aVC];
[self.myContainerView addSubview:self.aVC.view];
was always wrong. You MUST send didMoveToParentViewController: to the child controller after adding it to the parent. See my discussion here:
http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers
As for the rotation, it is probable that you're just doing all this too early. The app starts out in portrait and hasn't rotated yet to landscape when viewDidLoad is called. I give solutions to this problem here:
http://www.apeth.com/iOSBook/ch19.html#_rotation
Note the suggestion there that you wait until didRotateFromInterfaceOrientation: to finish setting up your view's appearance. I think you might be facing here the same issues I'm describing there.

Load UITableViewController programmatically and add view as subview

I want to load a UITableViewController inside a UIView because I want to change the view on button click (like UITabBar but with my own buttons). I'm using a storyboard and have defined a TableViewController with custom class "InitialTableViewController" and identifier "InitialView".
My code look like this:
#import "MyViewController.h"
#import "InitialTableViewController.h"
#interface MyViewController ()
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
InitialTableViewController *tableControl = [self.storyboard instantiateViewControllerWithIdentifier:#"InitialView"];
[[self view] addSubview:[tableControl view]];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
#end
The view starts and I can see my table but the code inside "InitialTableViewController" doesn't work.
What can I do?
Well, it would be easier to just have a normal UIViewController with a UIView as root of the Nib and then put a UITableView. My answer is based on your need to have buttons on that UIView.

iOS: UIViewController doesn't display another UIViewController using storyboards

I am tryin to display multiple UIViewController objects inside a single view. For the time being I want to display a single UIViewController object when the app loads. But the app screen appears blank, while it should be displaying a label inside the child view controller.
Here is what I did:
ParentViewController.h
#import <UIKit/UIKit.h>
#interface ParentViewController : UIViewController
{
UIViewController *child1Controller;
UIViewController *child2Controller;
}
#end
ParentViewController.m
#import "ParentViewController.h"
#import "Child1Controller.h"
#import "Child2Controller.h"
#interface ParentViewController ()
#end
#implementation ParentViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { ... }
- (void)viewDidLoad
{
child2Controller = [[Child2Controller alloc] init];
[self.view addSubview:child2Controller.view];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload { ... }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { ... }
#end
Then in the storyboard in interface builder
add 3 view controllers
assigned a class to each one of them ParentViewController, Child1Controller & Child2Controller
in Child2Controller object, added a UILabel inside View.
in Child2Controller.h defined the IBOutlet for UILabel and added a synthesize statement for the same in Child2Controller.m
finally in project-Info.plist set the main storyboard file
Did I miss something over here?
Starting from iOS 5 it's possible to take advantage of View Controller Containment. This is a new methodology that allows you to create a custom controller container like UINavigationController or UITabBarController.
In your case, this could be very useful. In fact, in your storyboard you could create the parent controller and the two child controllers. The parent could be linked to another scene while the two children are not linked. They are independent scenes that you can use within your parent controller.
For example in viewDidLoad method of your parent controller you could do the following:
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard *storyboard = [self storyboard];
FirstChildController *firstChildScene = [storyboard instantiateViewControllerWithIdentifier:#"FirstChildScene"];
[self addChildViewController:firstChildScene];
[firstChildScene didMoveToParentViewController:self];
}
Then in your FirstChildController override didMoveToParentViewController
- (void)didMoveToParentViewController:(UIViewController *)parent
{
// Add the view to the parent view and position it if you want
[[parent view] addSubview:[self view]];
CGRect newFrame = CGRectMake(0, 0, 350, 400);
[[self view] setFrame:newFrame];
}
And voilĂ ! You have a controller that contains one view that is managed by a child controller.
For further info see how-does-view-controller-containment-work-in-ios-5.
Hope it helps.

Resources