Making the view scrollable when keyboard appears - xcode

Managed to get it on my first view, but doesnt work on second view.
Here's what I did on both view, with only a slight differencs for debugging purposes in console
-(void) viewWillAppear:(BOOL)animated {
//---registers the notifications for keyboard---
// to see if keyboard is shown / not shown
[[NSNotificationCenter defaultCenter]
addObserver: self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
//---when the keyboard appears---
-(void) keyboardDidShow:(NSNotification *) notification {
if (keyboardIsShown) return;
NSLog(#"Keyboard is visible 1"); // debugger purpose "Keyboard is visible 2" on the second view.
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue *aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =
[self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view (with keyboard)---
CGRect viewFrame = [scrollview frame];
NSLog(#"%f", viewFrame.size.height);
viewFrame.size.height -= keyboardRect.size.height;
scrollview.frame = viewFrame;
NSLog(#"%f", keyboardRect.size.height);
NSLog(#"%f", viewFrame.size.height);
//---scroll to the current text field---
CGRect textFieldRect = [currentTextField frame];
[scrollview scrollRectToVisible:textFieldRect animated:YES];
keyboardIsShown = YES;
}
//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
//---obtain the size of the keyboard---
NSValue* aValue =
[info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect =
[self.view convertRect:[aValue CGRectValue] fromView:nil];
//---resize the scroll view back to the original size
// (without keyboard)---
CGRect viewFrame = [scrollview frame];
viewFrame.size.height += keyboardRect.size.height;
scrollview.frame = viewFrame;
keyboardIsShown = NO;
}
//---before the View window disappear---
-(void) viewWillDisappear:(BOOL)animated {
//---removes the notifications for keyboard---
[[NSNotificationCenter defaultCenter]
removeObserver: self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

You are registering for UIKeyboardDidShowNotification and UIKeyboardDidHideNotification notifications, and then deregistering for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification notifications. There's your error.

Related

Play Landscape Video in iOS8 with Landscape Correct Statusbar Inside Portrait App

Portrait App, loads video that displays in landscape.
in iOS7 all works fine (minor fix this is iOS6 too) but in iOS8 the statusbar, although showing in landscape actually shows at the size of a portrait statusbar so it only occupies the top left hand 60% of the video (if holding device in landscape).
The code I'm using is as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
[planTitleLabel setText:[[ProgrammeHandler sharedHandler]
stringForElement:kPlanStringElementPlanTitle
onDay:0 inPlan:selectedPlan]];
}
- (NSUInteger)supportedInterfaceOrientations{
return 0;
}
-(void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0) {
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:TRUE withAnimation:UIStatusBarAnimationNone];
}else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0 && [[UIDevice currentDevice].systemVersion floatValue] < 9.0) {
[[UIApplication sharedApplication] setStatusBarHidden:TRUE];
}
NSString *moviePath = [[ProgrammeHandler sharedHandler] pathForAsset:kAssetVideoIntro onDay:0 forPlan:selectedPlan];
CGSize screenBounds = [[UIScreen mainScreen] bounds].size;
[whiteFadeView setFrame:CGRectMake(0, 0, screenBounds.width, screenBounds.height)];
NSURL *videoURL = [NSURL fileURLWithPath:moviePath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[[moviePlayer view] setBackgroundColor:[UIColor blackColor]];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
//For viewing partially.....
moviePlayer.view.transform = CGAffineTransformMakeRotation(M_PI/2);
[moviePlayer setScalingMode:MPMovieScalingModeAspectFill];
[movieView setAlpha:0];
[moviePlayer.view setFrame:[movieView frame]];
moviePlayer.view.backgroundColor = [UIColor blackColor];
[movieView addSubview:moviePlayer.view];
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(hideControl)
name:MPMoviePlayerLoadStateDidChangeNotification
object:moviePlayer];
[UIView animateWithDuration:0.5f delay:0.50f
options:UIViewAnimationOptionCurveEaseIn
animations:^{[movieView setAlpha:1];} completion:^(BOOL fin){ }];
}
- (void) hideControl {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerNowPlayingMovieDidChangeNotification
object:moviePlayer];
[moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
}
I did see this question but don't think it applies in my case.
iphone: rotation in mpmovieplayercontroller?
Does any one know how to restore the statusbar to it's fullscreen 'width'?
Ok, after looking over this again today I noticed that there was an error that iOS7 seems happy with but it was correctly throwing an error in iOS8. For anyone else with the same issue:
The fix is simply to apply the transformation to the moviePlayer itself rather than the subview. e.g.
replace
moviePlayer.view.transform = CGAffineTransformMakeRotation(M_PI/2);
with
movieView.transform = CGAffineTransformMakeRotation(M_PI/2);

Content offset issue after watching video in iOS 8

I have a TabBarController, supportedInterfaceOrientations - UIInterfaceOrientationMaskPortrait. Navigation bar moves up under the status bar when I start watching video from one of tabs, rotate device to landscape and then exit from fullscreen. Why it's happens and how fix it?
This is a kind of hack, but works in my view controller contains UIWebView and fullscreen videos start from <video> tags.
You will see jerking of navigation bar because UIWindowDidBecomeHiddenNotification is called after video fullscreen window just disappear.
- (void)viewDidLoad
{
[super viewDidLoad];
// NOTE: I'm not sure, but MPMoviePlayerWillExitFullscreenNotification won't work
// [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidBecomeHidden:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidBecomeHidden:) name:UIWindowDidBecomeHiddenNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)windowDidBecomeHidden:(NSNotification *)notification
{
UIView *navbar = self.navigationController.navigationBar;
CGRect barFrame = navbar.frame;
barFrame.origin.y = 20;
self.navigationController.navigationBar.frame = barFrame;
UIView *navbarBack = nil;
for (UIView *view in navbar.subviews) {
if ([NSStringFromClass([view class]) isEqual:#"_UINavigationBarBackground"]) {
navbarBack = view;
break;
}
}
CGRect backFrame = navbarBack.frame;
backFrame.origin.y = -20;
backFrame.size.height = 64;
navbarBack.frame = backFrame;
[navbar.superview setNeedsLayout];
}
UIView *navbarBack = [navbar.subviews bk_match:^BOOL(UIView *view) {
return [NSStringFromClass([view class]) isEqual:#"_UINavigationBarBackground"];
}];
I have met the same issue.I tried to use js to get the "webkitendfullscreen" event,and then found navbar.frameOriginY = 0,which should be 20.

How to disable MasterView when the keyboard appears in the DetailView

I would like to know if it's possible (and how) when the keyboard appears in the DetailView, to disable any MasterView controls until it disappears. All of this happens in a split view based app of course.
---Update for Prince's solution---
MasterViewController.h
#property (strong, nonatomic) UIView *MasterView;
MasterViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MasterView=self.view;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
DetailViewController.m
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
((MasterViewController *)self.parentViewController).MasterView.userInteractionEnabled=NO;
return YES;
}
This code as is, crashes the app with an "Unknown Selector" error.
How do i bind delegates; Don't know if that's the problem or not. Any help?
Use UITextField's delegate and also bind delegates:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
MasterView.userInteractionEnabled = NO;
.......
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
MasterView.userInteractionEnabled = YES;
[textField resignFirstResponder];
return YES;
}
I found out a solution!
in MasterView viewDidLoad:
//---registers the notifications for keyboard---
// to see if keyboard is shown / not shown
[[NSNotificationCenter defaultCenter]
addObserver: self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
and then...:
//----------Handling Keyboard Appearence---
-(void) keyboardDidShow:(NSNotification *) notification {
[self.tableView setUserInteractionEnabled:NO];
}
//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
[self.tableView setUserInteractionEnabled:YES];
}
//---before the View window disappear---
-(void) viewWillDisappear:(BOOL)animated {
//---removes the notifications for keyboard---
[[NSNotificationCenter defaultCenter]
removeObserver: self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

mpmovieplayercontroller disappears as soon as it starts

When I click “play movie” the mpmovieplayer presents itself for half a second and I see it’s loading movie and the controls are there, but then it returns to main screen without playing the video.
How do I fix this?
Edit:
I have changed code and now the player stays up but is stuck on loading, it doesn’t play movie.
IS the problem: not preparing the movie for play? Or not stopping movie in background and then restarting it?
#import "ViewController.h"
#implementation ViewController
#synthesize moviePlayer;
-(IBAction)grabVid:(id)sender;
{
[self presentModalViewController:imagePicker animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[imagePicker setDelegate:self];
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
imagePicker.sourceType];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:imagePicker animated:YES];
{
[imagePicker dismissModalViewControllerAnimated:YES];
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
//[imagePicker dismissModalViewControllerAnimated:YES];
}
-(IBAction)playMovie:(id)sender
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[self.view addSubview:playercontroller.moviePlayer.view];
[playercontroller.moviePlayer setFullscreen:YES animated:YES];
playercontroller.moviePlayer.shouldAutoplay = NO;
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:playercontroller.moviePlayer];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
//[playercontroller.moviePlayer release];
}
- (void)dealloc {
//[super dealloc];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
#end
-(IBAction) playVideo
{
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayer.initialPlaybackTime = 0;
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
// playercontroller = nil;
}

move frame up when keyboard pops in Xcode

#import "LoginScreen.h"
#define kTabBarHeight 1
#define kKeyboardAnimationDuration 0.3
#implementation LoginScreen
#synthesize userName,password,loginButton,scrollView;
BOOL keyboardIsShown;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
keyboardIsShown = NO;
//make contentSize bigger than your scrollSize (you will need to figure out for your own use case)
// CGSize scrollContentSize = CGSizeMake(1024,700 );
// [scrollView setContentSize : scrollContentSize];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
}
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [boundsValue CGRectValue].size;
// resize the scrollview
CGRect viewFrame = self.scrollView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.size.height += (keyboardSize.height - kTabBarHeight);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = NO;
}
- (void)keyboardWillShow:(NSNotification *)n
{
// This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown.
if (keyboardIsShown) {
return;
}
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [boundsValue CGRectValue].size;
// resize the noteView
CGRect viewFrame = self.scrollView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.size.height -= (keyboardSize.height - kTabBarHeight);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:kKeyboardAnimationDuration];
[self.scrollView setFrame:viewFrame];
[UIView commitAnimations];
keyboardIsShown = YES;
}
- (IBAction) loginButton: (id) sender{
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc {
[scrollView release];
[super dealloc];
}
#end
Can anybody tell me whats wrong in code below. The original view doesn't move up even though i am subtracting the keyboards height to the frame heights.
The scrollView doesn't move up when keyboard pops in? Am I missing some code here.
Well, the problem is that you're not telling your code where your text field is to begin with, so it has no idea where it needs to scroll from. You probably thought that your self.scrollView.frame does it, but that only tells the code the size of your ScrollView, not that it needs to scroll or should scroll. Here's what I did to get mine to work.
Look at the connections for one of your text fields. Drag and drop from the "Did Begin Editing" and "Did End Editing" into your .h file to create IBAction function declarations. Xcode is going to put functions into the .m, but replace them with these:
//even though these functions don't reference the IBAction that we placed for the
"DidBeginEditing" sender for a text field, it will still call these functions.
We need to let the code know what text field we just touched so we can go through
the functions that reset the view size if the text field is under the keyboard.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
currentTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
currentTextField = nil;
}
That reminds me, you'll need to declare currentTextField as a UITextField * in the .m:
#interface ThirdView : UIViewController{
UITextField *currentTextField;
BOOL keyboardIsShown;
}
My viewDidLoad, keyBoardWasShown, etc are a bit different from yours, but I'll just put them all here so you can see how I got it to work:
//This is code you actually add to get the view to scroll.
You should first connect an outlet from the ScrollView to the .h file so these
functions become available.
- (void)viewDidLoad {
//standard screen size is 320 X 460
// ---set the viewable frame of the scroll view---
// scrollView.frame = CGRectMake(0, 0, 320, 460);
//---set the content size of the scroll view---
// [scrollView setContentSize:CGSizeMake(320, 615)];
//the status bar is 20 pixels tall
//the navigation bar is 44 pixels tall
//---set the viewable frame of the scroll view---
//Note: for some reason, the origin (0,44) doesn't take into account the status bar, but it works anyway. However, the height of the scroll view does take it into account. Wierd, but whatever. So you have to make y1 = 44, and y2 = 460-44-20.
scrollView.frame = CGRectMake(0, 44, 320, 416);
//---set the content size of the scroll view---
[scrollView setContentSize:CGSizeMake(320, 571)];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[self setPrincipal_Amt:nil];
[self setAPR:nil];
[self setYears:nil];
[self setScrollView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
if(keyboardIsShown)
return;
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
Notice that we've got to add 64 pixels to the keyboard height because the CGRect has
no idea that you have the status bar shown and a navigation bar within your view, so
you have to manually add it in.
CGRect aRect = self.view.frame;
aRect.size.height -= (kbSize.height + 64);
if (!CGRectContainsPoint(aRect, currentTextField.frame.origin) )
{
CGPoint scrollPoint = CGPointMake(0.0, currentTextField.frame.origin.y-kbSize.height + 64);
[scrollView setContentOffset:scrollPoint animated:YES];
}
keyboardIsShown = YES;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
keyboardIsShown = NO;
}
//
I hope that helps...even though you did post this a while ago :) It took me a while to get it to work too. Very frustrating.

Resources