add UINavigationController onto a UIScrollView - objective-c

I'm putting a NavigationController as a child VC on a ScrollView. Here's the code:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.automaticallyAdjustsScrollViewInsets = YES;
_scrollView = [UIScrollView newAutoLayoutView];
_scrollView.alwaysBounceVertical = YES;
_scrollView.delaysContentTouches = NO;
[self.view addSubview:_scrollView];
[_scrollView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
_contentView = [UIView newAutoLayoutView];
UIView *yellowView = [UIView newAutoLayoutView];
yellowView.backgroundColor = [UIColor yellowColor];
[_contentView addSubview:yellowView];
[yellowView autoPinEdgeToSuperviewEdge:ALEdgeTop];
[yellowView autoSetDimensionsToSize:CGSizeMake(kScreenWidth, kScreenHeight)];
[_scrollView addSubview:_contentView];
_settingsNavigationController = [UINavigationController new];
[self addChildViewController:_settingsNavigationController];
[_contentView addSubview:_settingsNavigationController.view];
[_settingsNavigationController didMoveToParentViewController:self];
[_settingsNavigationController.view autoSetDimensionsToSize:CGSizeMake(kScreenWidth, kScreenHeight)];
[_settingsNavigationController.view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:yellowView];
[_settingsNavigationController.view layoutSubviews];
[_contentView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:_settingsNavigationController.view];
[_contentView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[_contentView autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:self.view];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
PCConfigViewController *configViewController = [PCConfigController new];
configViewController.morning = YES;
configClockViewController.delegate = self;
[_settingsNavigationController setViewControllers:[NSArray arrayWithObject:configViewController] animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
I can scroll to the NavigationController and see its root view controller but it doesn't respond to touches, even though the NavigationController was added as a child view controller. Any ideas why, or how to make it responsive to touch?

Related

How to fade BOTH status bar and navigation bar LIKE Photos.app, on iOS 7

Basically, it's a very simple demand, but I've tried several methods and none of them works as expected. The closest functioning snippet is:
#import "ViewController.h"
#implementation ViewController
- (void)dealloc
{
[scrollView release];
scrollView = nil;
[super dealloc];
}
- (id)init
{
if (self = [super init])
{
self.title = #"Pictures";
scrollView = [[UIScrollView alloc] init];
scrollView.delegate = self;
scrollView.frame = CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 10, scrollView.bounds.size.height);
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.pagingEnabled = YES;
scrollView.userInteractionEnabled = YES;
scrollView.backgroundColor = [UIColor blackColor];
self.wantsFullScreenLayout = YES;
self.automaticallyAdjustsScrollViewInsets = NO;
isHidden = NO;
}
return self;
}
- (void)viewDidLoad
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[scrollView addGestureRecognizer:tapGesture];
[tapGesture setNumberOfTapsRequired:1];
[tapGesture release];
for (int i = 0; i < 10; i++)
{
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"/path/to/%d.png", i + 1]];
UIImageView *imageView= [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0.0f, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = image;
[scrollView addSubview:imageView];
[image release];
[imageView release];
}
[self.view addSubview:scrollView];
}
- (void)tap:(UITapGestureRecognizer *)gesture
{
[UINavigationBar setAnimationDuration:1.0];
[UINavigationBar beginAnimations:#"HideTopBars" context:nil];
isHidden = !isHidden;
// [self setNeedsStatusBarAppearanceUpdate];
self.navigationController.navigationBar.alpha = isHidden ? 0.0f : 1.0f;
[UINavigationBar commitAnimations];
}
- (void)scrollViewDidScroll:(UIScrollView *)view
{
// further operation
}
- (BOOL)prefersStatusBarHidden
{
return isHidden;
}
#end
Without "[self setNeedsStatusBarAppearanceUpdate]", navigation bar does fade as expected, but texts on status bar remain visible, which I guess is because status bar takes navigation bar as it's background image and status bar itself doesn't fade; With "[self setNeedsStatusBarAppearanceUpdate]", the texts also fade, but navigation bar's animation becomes sliding in/out from the top of the screen along with fade effect. I've also tried to move "[self setNeedsStatusBarAppearanceUpdate]" into prefersStatusBarHidden, but that just made navigation bar visible forever. I believe this is not an odd demand, so I bet there're better and simpler solutions. Any idea?

uiview animatewithduration completion continues after view is dismissed

I have a modelViewController that contains a UIView animation. When the animation block finishes it calls itself, thus looping.
When I dismiss the modelViewController (dismissInfo) which calls [_starView removeFromSuperview], the function gets called over and over very rapidly with the NSLog line being printed multiple times a second.
#implementation InfoVC
{
NSArray *imgs;
NSString *currentImg;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_imageview.contentMode = UIViewContentModeLeft;
_imageviewUnder.contentMode = UIViewContentModeLeft;
imgs = [NSArray arrayWithObjects:
#"01.jpg",
#"02.jpg",
#"03.jpg",
#"04.jpg",
#"05.jpg",
#"06.jpg",
nil];
_imgInt = (arc4random()%6);
[self initialImage];
}
- (void)viewWillAppear:(BOOL)animated{
}
- (void)viewDidAppear:(BOOL)animated{
NSLog(#"viewDidAppear");
}
- (void)initialImage
{
_starView.contentMode = UIViewContentModeLeft;
_imageviewUnder.contentMode = UIViewContentModeLeft;
currentImg = [imgs objectAtIndex:_imgInt];
UIImage *image = [UIImage imageNamed:currentImg];
_starView = [[UIImageView alloc] initWithImage:image];
// Size the image view to the image (it's bigger)
_starView.bounds = CGRectMake(0.0, 44.0, 416.0, 416.0);
NSLog(#"tarView.center %#", NSStringFromCGPoint(_starView.center) );
_starView.alpha=1;
int nextImgInt = _imgInt + 1 ;
if (nextImgInt>5)
{
nextImgInt=0;
}
NSString *nextImg = [imgs objectAtIndex:nextImgInt];
UIImage *nextImage = [UIImage imageNamed:nextImg];
[_imageviewUnder setImage:nextImage];
[self.view sendSubviewToBack:_imageviewUnder];
_imgInt++;
if (_imgInt>5) {
_imgInt=0;
}
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView animateWithDuration:7.6f
delay:0.1f
options:UIViewAnimationCurveLinear
animations:^{
[_starView setCenter:CGPointMake(112, 208)];
[_starView setAlpha:0.0f];
}
completion:^(BOOL finished){
[_starView removeFromSuperview];
[self initialImage];
}];
[self.view insertSubview:_starView atIndex:1];
}
- (void)visitTwitter
{
NSURL *URL = [NSURL URLWithString:#"http://twitter.com/"];
SVWebViewController *webViewController = [[SVWebViewController alloc] initWithURL:URL];
[self.navigationController pushViewController:webViewController animated:YES];
}
- (IBAction)dismissInfo:(id)sender
{
[self cleanup];
[self dismissModalViewControllerAnimated:YES];
}
- (void)cleanup
{
[_starView.layer removeAllAnimations];
[self.view.layer removeAllAnimations];
[_starView removeFromSuperview];
}
- (void)viewDidUnload
{
[self cleanup];
[super viewDidUnload];
}
#end
Make yourself an animating flag, set true in viewDidLoad, and false in cleanup. Then check it in your initialImage method:
if ( ! animating )
return;
This is the second time I've come across this. One possible cause is the method being initialised in viewDidLoad but the subview and image not being ready in time (it seems the first pass of the animation loop doesn't work adding a ghost to the machine).
I moved the call to ViewDidAppear, making sure to display an initialisation image to stop the white flash. At last an infinite animation in a modal window
- (void)viewDidLoad
{
[super viewDidLoad];
_imageview.contentMode = UIViewContentModeLeft;
_imageviewUnder.contentMode = UIViewContentModeLeft;
imgs = [NSArray arrayWithObjects:
#"01.jpg",
#"02.jpg",
#"03.jpg",
#"04.jpg",
#"05.jpg",
#"06.jpg",
nil];
_imgInt = (arc4random()%6);
currentImg = [imgs objectAtIndex:_imgInt];
UIImage *image = [UIImage imageNamed:currentImg];
[_imageviewUnder setImage:image];
}
- (void)viewDidAppear:(BOOL)animated{
[self initialImage];
}
- (void)initialImage
{
currentImg = [imgs objectAtIndex:_imgInt];
UIImage *image = [UIImage imageNamed:currentImg];
_starView = [[UIImageView alloc] initWithImage:image];
// Size the image view to the image (it's bigger)
_starView.bounds = CGRectMake(0.0, 44.0, 416.0, 416.0);
NSLog(#"initialImage");
_starView.alpha=1;
[self.view insertSubview:_starView atIndex:1];
int nextImgInt = _imgInt + 1 ;
if (nextImgInt>5)
{
nextImgInt=0;
}
NSString *nextImg = [imgs objectAtIndex:nextImgInt];
UIImage *nextImage = [UIImage imageNamed:nextImg];
[_imageviewUnder setImage:nextImage];
[self.view sendSubviewToBack:_imageviewUnder];
_imgInt++;
if (_imgInt>5) {
_imgInt=0;
}
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:7.6];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
//[UIView setAnimationDidStopSelector:#selector(initialImage) ];
[_starView setCenter:CGPointMake(112, 208)];
[_starView setAlpha:0.0f];
[UIView commitAnimations];
[self performSelector:#selector(initialImage) withObject:nil afterDelay:7.6];
}
- (IBAction)dismissInfo:(id)sender
{
[self cleanup];
[self dismissModalViewControllerAnimated:YES];
}
- (void)cleanup
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[_starView.layer removeAllAnimations];
[self.view.layer removeAllAnimations];
[_starView removeFromSuperview];
}
If you just want to create a looping animation, the best thing to do is use the option: 'UIViewAnimationOptionRepeat'. For example:
[UIView animateWithDuration:2.0
delay:0.0f
options:UIViewAnimationOptionRepeat
animations:^{
[myUIViewThing setCenter:CGPointMake(myUIViewThing.center.x - 100, myUIViewThing.center.y)];
}
completion:nil];

iOS5 GestureRecognizer fires only on the second tap

Would anyone have an idea why Tap gesture recognizer is called only after second tap in ios5, although in ios6 it works fine after one tap.
The code works in ios6, I tried this code also in separate project and works as well for ios5, I think it's linked to the project settings, but I cannot find out which.
My Code is:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = questTitle;
//add coretextview
scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
coreTextView = [[FTCoreTextView alloc] initWithFrame:CGRectMake(20, 20, 280, 0)];
coreTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// set text
[coreTextView setText:[self textForView]];
// set styles
[coreTextView addStyles:[self coreTextStyle]];
// set delegate
[coreTextView setDelegate:self];
[coreTextView fitToSuggestedHeight];
scrollView.scrollEnabled = TRUE;
[scrollView addSubview:coreTextView];
[scrollView setContentSize:CGSizeMake(CGRectGetWidth(scrollView.bounds), CGRectGetHeight(coreTextView.frame) + 40)];
[self.view addSubview:scrollView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(pan:)];
tapGesture.delegate = (id <UIGestureRecognizerDelegate>)self;
[coreTextView addGestureRecognizer:tapGesture];
tapGesture.numberOfTapsRequired = 1; // How many taps
tapGesture.numberOfTouchesRequired = 1;
coreTextView.userInteractionEnabled = YES;
tapGesture.cancelsTouchesInView = NO;
scrollView.userInteractionEnabled = YES;
scrollView.exclusiveTouch = NO;
scrollView.delegate = self;
[tapGesture release];
}
- (void)pan:(UITapGestureRecognizer*)sender {
NSLog(#"Pan tap");
}
- (BOOL)gestureRecognizerShouldBegin:(UITapGestureRecognizer *)gestureRecognizer{
NSLog(#"tap");
return YES;
}
- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UITapGestureRecognizer *)otherGestureRecognizer{
NSLog(#"tap");
return YES;
}
- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
NSLog(#"tap");
return YES;
}

Hijack "swipe to next page" in UIScrollView

i'd like to prevent scrolling on the 3rd page of my UIScrollview and "hijack the swipe" gesture to trigger sth. else. Afer this action I'd like to reactive scrolling.
This does not work.
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if(scrollView.contentOffset.x == self.view.frame.size.width * 2 ) {
// disable scrolling
scrollView.scrollEnabled = NO;
}
}
// hijack the next scrolling event
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
This delegate is not called when scrollEnabled = NO
Thanks for helping
EDIT EventHandler ist not called ;-(
- (void)viewDidLoad
{
[super viewDidLoad];
// Default background color
self.view.backgroundColor = [UIColor redColor];
// Create scroll view
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
recognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[scrollView addGestureRecognizer:recognizer];
[recognizer release];
[scrollView delaysContentTouches];
// Create subviews (pages)
NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++) {
// x pos
CGFloat yOrigin = i * self.view.frame.size.width;
// Create subview and add to scrollView
UIView *pageView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
pageView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scrollView addSubview:pageView];
[pageView release];
}
// Set contentsize
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
// Add scrollView to view and release
[self.view addSubview:scrollView];
[scrollView release];
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
NSLog(#"swipe!!!!");
scrollView.scrollEnabled = YES;
}
If you disable the scroll view:
scrollView.scrollEnabled = NO;
it is unavoidable that the delegate method is not called, so you need an alternative way to handle the swipe while in hijack mode. One thing you could try is using an UISwipeGestureRecognizer: instead of simply disabling scrolling, you could associate a UISwipeGestureRecognizer to you view and handle the swipe from the handler method:
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipeFrom:)];
recognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:recognizer];
and in handleSwipeFrom you would reenable scrolling:
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
// do your hijack here
scrollView.scrollEnabled = YES;
}

Issues with UISplitViewController and modal UIViewControllers

I have a really strange UI glitch in my iPhone/iPad app. Because I wanted to find the cause of it, I created a new project with as little code as possible. The important code is below.
Basically, I have a UISplitViewController containing two UIViewController subclasses. When a button is tapped, the first one presents modally and as UIModalPresentationFormSheet a UIViewController subclass called Modal. In there, when a button is tapped, another UIViewController subclass called Text is presented, this time as UIModalPresentationFullScreen. In Text, there is a UITextView. When it is tapped, everything is alright, but when the iPad is rotated, I get this:
The white part is the Text view controller, the red part in the background is ViewControllerTwo.
Does anybody have any idea why this happens? And what I can do to fix it?
Here is the Project: MediaFire
Here is the relevant source code:
// AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ViewControllerOne *one = [[ViewControllerOne alloc] init];
ViewControllerTwo *two = [[ViewControllerTwo alloc] init];
UISplitViewController *split = [[UISplitViewController alloc] init];
split.viewControllers = [NSArray arrayWithObjects:one, two, nil];
self.window.rootViewController = split;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
// ViewControllerOne.m
#import "Modal.h"
#implementation ViewControllerOne
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blueColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(30, 30, 44, 44);
[button addTarget:self action:#selector(buttonTapped) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonTapped
{
Modal *modalOne = [[Modal alloc] init];
modalOne.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalOne animated:YES];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Modal.m
#import "Text.h"
#implementation Modal
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *buttonOne = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonOne.frame = CGRectMake(30, 30, 44, 44);
buttonOne.tag = 1;
[buttonOne addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonTwo.frame = CGRectMake(30, 100, 44, 44);
buttonTwo.tag = 2;
[buttonTwo addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonOne];
[self.view addSubview:buttonTwo];
}
- (void)buttonTapped:(UIButton *)button
{
if (button.tag == 1)
{
Text *text = [[Text alloc] init];
text.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentModalViewController:text animated:YES];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// Text.m
#implementation Text
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
You may need to present the modal views from the UISplitViewController instead of it's subviews...
UISplitViewController *splitViewController = [(AppDelegate *)[[UIApplication sharedApplication] delegate] splitViewController];
[splitViewController presentModalViewController:modal animated:YES];