I have a UITableViewController that I want to add iAds to. I want the ad to display at the bottom of the screen at all times. I have followed the answer here: https://stackoverflow.com/a/9857798/2584268 and have achieved this behavior. However, the ad is hidden in the beginning and only appears when the user scrolls the table. How can I get the ad to appear at the bottom of the screen when the view loads, not just when the user scrolls?
To show the ad when the view appears, I add the banner view on viewDidAppear.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_myIAD = [[self appdelegate] myIAD];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//iPhone code
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 50, 320, 50);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 114, 320, 50);
}
}
else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 32, 480, 32);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 84, 480, 32);
}
}
}
else
{
//iPad code
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 66, 768, 66);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 130, 768, 66);
}
}
else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 66, 1024, 66);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 130, 1024, 66);
}
}
}
[self.view addSubview:_myIAD];
}
There is a ton of code in there to deal with iOS6.1 and iPads and iPhones, but it works real well. I always like to use the statusBarOrientation to find the orientation, it works better.
As you can see there is a line
_myIAD = [[self appdelegate] myIAD];
I actually make the banner in the app delegate and use it in all the view controllers. To handle rotation, I register for a notification in viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateUI:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
And basically use the same code in the viewDidAppear in the method updateUI:
The only other thing I did was to put in this code to keep the iAd at the bottom.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = _myIAD.frame;
frame.origin.y = _myTableView.contentOffset.y + _myTableView.frame.size.height-_myIAD.frame.size.height; // Move view to the bottom on scroll
_myIAD.frame = frame;
[_myTableView bringSubviewToFront:_myIAD];
}
Related
everybody.
Guys, I have a problem with status bar in my project..
In default , in app status bar should hidden. This is work, but one view have a bug with status bar like on screenshot.
On main menu it's have black color and don't hidden(
In singleton I'm initialise background image for different devices, and this is work for all views in app))
This is my code singleton init background:
// background return
- (UIColor *)mainBackground:(UIInterfaceOrientation)orientation {
UIColor *color;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundIpad.png"]];
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundLandspIpad.png"]];
}
} else {
color = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
}
return color;
}
//return wight screen
- (float)wightScreen {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
return 768;
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
return 1024;
}
}
return wight();
}
//return rect view all for screens
- (CGRect)refreshPoints {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
return CGRectMake(0, 0, 768, 1024);
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {
return CGRectMake(0, 0, 1024, 768);
}
} else {
return CGRectMake(0, 0, 320, height());
}
return CGRectMake(0, 0, 768, 1024);
}
And this is code in controller view, where I'm have a problem:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return [[Singleton sharedInstance] getSelf:#"MainMenuController"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[PostRequest sharedInstance] userInfo];
self.view.backgroundColor = [[Singleton sharedInstance] mainBackground:[[UIApplication sharedApplication] statusBarOrientation]];
// if (iphone4()) {
// [self newRectNiewIphone];
// }
}
I'm hope in yours help!
This is screenshot with bug status bar
http://i.stack.imgur.com/nfiZ3.png
And screenshot, how on another views
http://i.stack.imgur.com/wVadC.png
You need to set
self.automaticallyAdjustsScrollViewInsets=NO;
On the viewController in the viewDidLoad method.
I am running a phonegap application on iOS7 and I need to resize the UIView when the keyboard opens and when the orientation of the device changes.
I have some of the resize working but I am stuck when the orientation of the device is LandscapeRight, the first time the keyboard opens it slides up then the second time it slides it up even more.
I have search similar answers on stackoverflow and none have solved what I am looking for. So, please don't just copy and paste a solution from else where, thanks.
MainViewController.m
int screenWidth;
int screenHeight;
float keyboard_offset = 80.0;
- (void)viewWillAppear:(BOOL)animated
{
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
CGRect viewBounds = [self.webView bounds];
viewBounds.origin.y = 20;
viewBounds.size.height = viewBounds.size.height - 20;
self.webView.frame = viewBounds;
self.webView.backgroundColor = [UIColor blackColor];
self.webView.opaque=NO;
screenWidth = self.view.frame.size.width;
screenHeight = self.view.frame.size.height;
}
[super viewWillAppear:animated];
}
- (void)orientationChange:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationLandscapeLeft:
[self.view setFrame: CGRectMake(-keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
break;
case UIDeviceOrientationLandscapeRight:
[self.view setFrame: CGRectMake(keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
break;
case UIDeviceOrientationPortrait:
case UIDeviceOrientationPortraitUpsideDown:
[self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
break;
default:
break;
};
}
- (void)keyboardDidShow:(NSNotification *) notification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChange:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft)
{
[self.view setFrame: CGRectMake(-keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
}
else if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)
{
[self.view setFrame: CGRectMake(keyboard_offset, 0, screenWidth + keyboard_offset, screenHeight)];
}
else
{
[self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
}
}
- (void)keyboardDidHide:(NSNotification *) notification
{
[self.view setFrame: CGRectMake(0, 0, screenWidth, screenHeight)];
}
AppDelegate.m
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
/*
Orientation change for iOS7
*/
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
self.window.clipsToBounds = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
/**
* Change the status bar color on orientation change.
*/
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationLandscapeLeft:
self.window.frame = CGRectMake(-20, 0, screenWidth, screenHeight);
self.window.bounds = CGRectMake(-20, 0, screenWidth, screenHeight);
break;
case UIDeviceOrientationLandscapeRight:
self.window.frame = CGRectMake(20, 0, screenWidth, screenHeight);
self.window.bounds = CGRectMake(20, 0, screenWidth, screenHeight);
break;
case UIDeviceOrientationPortrait:
self.window.frame = CGRectMake(0, 20, screenWidth, screenHeight);
self.window.bounds = CGRectMake(0, 20, screenWidth, screenHeight);
break;
case UIDeviceOrientationPortraitUpsideDown:
self.window.frame = CGRectMake(0, -20, screenWidth, screenHeight);
self.window.bounds = CGRectMake(0, -20, screenWidth, screenHeight);
break;
default:
break;
};
}
CDVViewController.m
/*
Change the status bar color to the old style
*/
-(UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad
{
[super viewDidLoad];
/*
Only perform these actions for iOS 7 or above
*/
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
[self setNeedsStatusBarAppearanceUpdate];
}
}
I have a tabbar app in which I need to show a popover from the tabBarItem by clicking on it. So, I in my appDelegate:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if (viewController == [self.objTabBarController.viewControllers objectAtIndex:2])
{
if (self.settingsPopover == nil) {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main - iPad" bundle: nil];
SettingsViewController *settingsController = [mainStoryboard instantiateViewControllerWithIdentifier: #"SettingsPopover"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:settingsController];
self.settingsPopover = [[UIPopoverController alloc] initWithContentViewController:navigationController];
self.settingsPopover.delegate = self;
CGSize screenSize;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){
screenSize = CGSizeMake ([UIScreen mainScreen].applicationFrame.size.width, [UIScreen mainScreen].applicationFrame.size.height);
} else {
screenSize = CGSizeMake ([UIScreen mainScreen].applicationFrame.size.height, [UIScreen mainScreen].applicationFrame.size.width);
}
NSLog(#"WIDTH = %f, HEIGHT = %f", screenSize.width, screenSize.height);
screenSize.height -= 50;
screenSize.width = screenSize.width / 2 + 105;
CGRect rect = CGRectMake(screenSize.width, screenSize.height, 1, 1);
[self.settingsPopover presentPopoverFromRect:rect inView:self.window.rootViewController.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
} else {
[self.settingsPopover dismissPopoverAnimated:NO];
self.settingsPopover = nil;
}
return NO;
}
}
return YES;
}
So it displays correctly. But I need to redraw the popover after the change of the device orientation. For this purposes I made:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleDidChangeStatusBarOrientationNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
- (void) handleDidChangeStatusBarOrientationNotification:(NSNotification *)notification;
{
if (self.settingsPopover)
{
CGSize screenSize;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){
screenSize = CGSizeMake ([UIScreen mainScreen].applicationFrame.size.width, [UIScreen mainScreen].applicationFrame.size.height);
} else{
screenSize = CGSizeMake ([UIScreen mainScreen].applicationFrame.size.height, [UIScreen mainScreen].applicationFrame.size.width);
}
NSLog(#"WIDTH = %f, HEIGHT = %f", screenSize.width, screenSize.height);
screenSize.height -= 50;
screenSize.width = screenSize.width / 2 + 105;
CGRect rect = CGRectMake(screenSize.width, screenSize.height, 1, 1);
[self.settingsPopover presentPopoverFromRect:rect inView:self.window.rootViewController.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
}
So, the NSLog shows the correct screenSize after the changing the orientation but the popover shows in incorrect place. Can anyone help me to solve this?
For that you have to close your Popover first and then show it again as I have done to solve my problem
In case anyone is looking for an answer in Swift 3...
1) Register for orientation change events.
NotificationCenter.default.addObserver(self, selector: #selector(screenRotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
2) Inside your view controller being presented as a popover...
func screenRotated() {
// Redraw the popover.
DispatchQueue.main.async {
self.preferredContentSize = CGSize(width: UIScreen.main.bounds.width, height: self.view.frame.height)
}
}
I have a search button and when user clicks it I am displaying UISearchBar programmatically. Width of Searchbar is (200) not full screen. I am searching like a MSWord and displaying the content in searchbar' tableview. But when user is in middle of search and changed orientation how should I control the search bar width, i.e., self.searchDisplayController.searchBar.frame and self.searchDisplayController.searchResultsTableView.frame.
Below is the piece of code that used to create the searchbar:
-(IBAction)searchBar:(id)sender
{
if(!searching)
{
searching = YES;
sBar.frame = CGRectMake(500, 50,250, 44);
[self.view addSubview:sBar];
[searchController setActive:YES animated:YES];
[sBar becomeFirstResponder];
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
}
else
{
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
}
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
/*
Bob: Because the searchResultsTableView will be released and allocated automatically, so each time we start to begin search, we set its delegate here.
*/
[self.searchDisplayController.searchResultsTableView setDelegate:self];
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:204/255.0 alpha:1.0]];
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
/*
Hide the search bar
*/
float animateTime = 0.3;
CGRect viewFrame = sBar.frame;
viewFrame.origin.y -= (viewFrame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animateTime];
sBar.frame = viewFrame;
[UIView commitAnimations];
[self performSelector:#selector(animationDone) withObject:nil afterDelay:0.3];
}
-(void)animationDone
{
[sBar removeFromSuperview];
searching = NO;
}
-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
tableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
tableView.frame = CGRectMake(500, 100, 250, 400);
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
currentOrientation = toInterfaceOrientation;
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
//Search
btnSearch.frame = CGRectMake(920, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
self.searchDisplayController.searchResultsTableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
//Search
btnSearch.frame = CGRectMake(666, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
self.searchDisplayController.searchResultsTableView.frame = CGRectMake(500, 100, 250, 400);
}
}
I am able to create and functionality which is working fine. But only issue is when user change the orinentation its not properly setting searchbar' width.
My application needs to be delivered this friday. Please answere me asap.
Answering my question to close this thread as well to help others.
(1) In willRotateToInterfaceOrientation its able to set UISearchBar origin. Width is coming as 768 (full screen width of iPad in Portrait) but origin seems to be set here.
(2) After orientation change finished we can set the search bar width. With small animation it will be smooth resize.
(3) I am calling resetFrame at both pre & post orientation APIs to avoid some sudden JURK kind for searchBar.
Below is the solution i used for my app..
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
currentOrientation = toInterfaceOrientation;
[self resetSearchResourcesFrames]; // Set the origin of UISearchBar
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[UIView beginAnimations:#"" context:nil]; //Animation for smooth resizing of search tableview only
[UIView setAnimationDuration:0.2];
[self resetSearchResourcesFrames]; // Width rotation only possible after rotation
[UIView commitAnimations];
}
-(void)resetSearchResourcesFrames
{
if(([buttonGridViewController sharedInstance].currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
([buttonGridViewController sharedInstance].currentOrientation == UIInterfaceOrientationLandscapeRight))
{
//Search
btnSearch.frame = CGRectMake(920, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
self.searchController.searchResultsTableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
//Search
btnSearch.frame = CGRectMake(666, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
self.searchController.searchResultsTableView.frame = CGRectMake(500, 100, 250, 400);
}
}
I'm working with an MPMoviePlayerController which renders its UIView to the right dimensions on landscape, but when rotating to portrait it is simply not resizing. I think it's parent UIView is the one not resizing. How can I set this up?
I followed the instructions that #Alex Reynolds mentions in UIView autoresizingmask not working for me. This showed me that the view does resize when rotated. I still have the problem that when the UIView for the UIViewController that holds the player is loaded, if the orientation of the device is landscape, they it renders to the right frame, but if the devise is on portrait by the time it is loaded it is not resized to it. With #Alex Reynolds' answer, all I have to do is rotate the device once and it will start resizing properly after that.
It is still bad that it won't resize the first time. Has this happened to anyone before? If so, any input is greatly appreciated.
Have you set the MPMoviePlayerController's view's autoresizingMask appropriately? Is its superview's autoresizesSubviews property set to YES? (and likewise, does this superview also resize when rotating? I like to set colourful background colours for my views during testing to verify that they resize correctly when autorotating.)
If it's still not working after checking those properties, you can always set the movie player's view's frame property manually. The super view's layoutSubviews method is generally the best place to do that, but if it's not a view you've manually subclassed, you can also do it in the view controller.
- (void)viewDidLoad
{
[super viewDidLoad];
[self play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(IBAction)dismiss:(id)sender
{
//[self.view removeFromSuperview];
_moviePlayer =nil;
[self dismissViewControllerAnimated:YES completion:nil];
}
// Do any additional setup after loading the view from its nib.
-(void)play
{
NSURL *url = [NSURL URLWithString:#"stringurlvideo"];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:)
// name:MPMoviePlayerPlaybackDidFinishNotification
// object:_moviePlayer];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFull:)
// name:MPMoviePlayerDidEnterFullscreenNotification
// object:_moviePlayer];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidExit:)
// name:MPMoviePlayerDidExitFullscreenNotification
// object:_moviePlayer];
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
if([[UIScreen mainScreen] bounds].size.height==568)
{
[_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
}
else
{
[_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];
}
[self.view addSubview:_moviePlayer.view];
//[_moviePlayer.view setCenter:self.view.center];
[_moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFull:(NSNotification*)notification
{
}
-(void)moviePlayBackDidExit:(NSNotification*)notification
{
////[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
-(void)viewWillAppear:(BOOL)animated
{
/* if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
{
[_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
CGRect rect=[bar frame];
rect.size.width=self.view.frame.size.width;
[bar setFrame:rect];
}
else
{
CGRect rect=[bar frame];
rect.size.width=480;
[bar setFrame:rect];
[_moviePlayer.view setFrame:CGRectMake(0,44, 480, 320-44)];
}*/
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player
respondsToSelector:#selector(setFullscreen:animated:)])
{
[player.view removeFromSuperview];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}
- (BOOL)shouldAutorotate
{
return YES;
}
/*- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}*/
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(toInterfaceOrientation==UIInterfaceOrientationPortrait && toInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
{
if([[UIScreen mainScreen] bounds].size.height==568)
{
[_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
}
else
{
[_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];
}
CGRect rect=[bar frame];
rect.size.width=self.view.frame.size.width;
[bar setFrame:rect];
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight)
{
CGRect rect=[bar frame];
if([[UIScreen mainScreen] bounds].size.height==568)
{
rect.size.width=568;
}
else{
rect.size.width=480;
}
[bar setFrame:rect];
[_moviePlayer.view setFrame:CGRectMake(0,44, rect.size.width, 320-44)];
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
/* if(fromInterfaceOrientation==UIInterfaceOrientationPortrait && fromInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
{
CGRect rect=[bar frame];
rect.size.width=568;
[bar setFrame:rect];
[_moviePlayer.view setFrame:CGRectMake(0,44, 480, 320-44)];
}
else
{
[_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
CGRect rect=[bar frame];
rect.size.width=self.view.frame.size.width;
[bar setFrame:rect];
}*/
}
- (void)didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation ==UIInterfaceOrientationLandscapeRight)
{
CGRect rect=[bar frame];
if([[UIScreen mainScreen] bounds].size.height==568)
{
rect.size.width=568;
}
else{
rect.size.width=480;
}
[bar setFrame:rect];
[_moviePlayer.view setFrame:CGRectMake(0,44, rect.size.width, 320-44)];
}
else if(orientation==UIInterfaceOrientationMaskPortrait && orientation!=UIDeviceOrientationPortraitUpsideDown)
{
if([[UIScreen mainScreen] bounds].size.height==568)
{
[_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
}
else
{
[_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];
}
CGRect rect=[bar frame];
rect.size.width=self.view.frame.size.width;
[bar setFrame:rect];
}
}