I am experiencing a little problem. I'm trying to develop an app with UINavigationController bar but it is not rotating although my tableView is.
Just a little introduction so you know what my application is right now : I have a UITableView which is created in IB and linked in my class ViewController.h and a UINavigationController created in the appDelegate.h. All the items in the tableView can be clicked and it will slide to another viewController but this part is not important for now. I'm just trying to support rotation in the main view for now.
I create the UINavigationController in my AppDelegate.h like this:
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigation;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UIViewController *viewController;
And then in AppDelegate.m I do this :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
ViewController *viewController = [[ViewController alloc] init];
viewController.title = #"Agrinuvo";
[navigation pushViewController:viewController animated:NO];
navigation = [[UINavigationController alloc] initWithRootViewController:viewController];
navigation.navigationBar.tintColor = [UIColor darkGrayColor];
[[UIBarButtonItem appearance] setTintColor:[UIColor orangeColor]];
[_window addSubview:navigation.view];
}
I have tried in my ViewController.m viewDidLoad method these :
self.navigationController.navigationBar.autoresizesSubviews = YES;
self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
I am able to resize the UINavigationBar but I cannot rotate it for the life of me (still in ViewController.m) :
-(void) orientationChanged
{
CGRect frame = self.navigationController.navigationBar.frame;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)
{
self.view.transform = CGAffineTransformMakeRotation(0);
frame.size.height = 44;
frame.origin.y = 15;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * 1);
frame.size.height = 44;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * -0.5);
frame.size.height = 32;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
frame.size.height = 32;
}
self.navigationController.navigationBar.frame = frame;
}
Ah yes, I also tried this method in all of my controllers and it is returning YES in all of them but the NavigationBar still does not rotate.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
So I'm wondering if it is actually possible to rotate a NavigationBar. Also do I create it like it should be created?
Should I be initiating my UINavigationController this way or should I be creating it in IB and link it to a .h class?
Sorry for the long read and thank you for any help you can provide
You should make your navigation controller the root view controller of the window. Also, in your applicationDidBecomeActive:method, you have this line: [navigation pushViewController:viewController animated:NO]; which you use before you've instantiated navigation, so that line isn't doing anything.
Related
I need to be able to select a storyboard with its initial UIViewController upon application launch per boolean choice. Each storyboard works... per .plist (original) setup.
So I removed the default initial storyboard entry in the app's plist; and attempted to do it manually.
What I got is a black screen on my device.
The variables 'storyboard', 'controller', 'window' are all non-nil. Yet I get no screen. Why?
Here's the code:
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIColor *blissRed = [UIColor colorWithRed:228.0/255.0 green:0.0 blue:27.0/255.0 alpha:1.0];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.tintColor = blissRed;
BOOL introStoryboard = YES; // ...artificial condition for discussion.
UIStoryboard *storyboard;
UIViewController *controller;
if (introStoryboard) {
storyboard = [UIStoryboard storyboardWithName:#"Intro" bundle:nil];
controller = [storyboard instantiateViewControllerWithIdentifier:kIntroStoryboardBeginning];
} else {
storyboard = [UIStoryboard storyboardWithName:#"Bliss" bundle:nil];
controller = [storyboard instantiateViewControllerWithIdentifier:kBlissStoryboardBeginning];
}
[self.window setRootViewController:controller];
return YES;
}
#end
you just forgot the following line before return
[self.window makeKeyAndVisible];
I was in the situation once. I resolved it by doing-
[navigationController pushViewController:controller animated:YES];
So what u need to do is -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIColor *blissRed = [UIColor colorWithRed:228.0/255.0 green:0.0 blue:27.0/255.0 alpha:1.0];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.tintColor = blissRed;
BOOL introStoryboard = YES; // ...artificial condition for discussion.
UIStoryboard *storyboard;
UIViewController *controller;
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
if (introStoryboard) {
storyboard = [UIStoryboard storyboardWithName:#"Intro" bundle:nil];
controller = [storyboard instantiateViewControllerWithIdentifier:kIntroStoryboardBeginning];
} else {
storyboard = [UIStoryboard storyboardWithName:#"Bliss" bundle:nil];
controller = [storyboard instantiateViewControllerWithIdentifier:kBlissStoryboardBeginning];
}
[navigationController pushViewController:controller animated:YES];
return YES;
}
I encounter a problem using UITabBarItem and UIButton in my application. My button is inside a UITabBarItem. When I press it I want to be pushed to another controller to display a PDF.
Here is a piece of code that works in other cases :
- (void)viewDidLoad {
UIImage* imageButton = [UIImage imageNamed:#"pdf-button.png"];
UIButton *buttonPDF = [UIButton buttonWithType:UIButtonTypeCustom];
buttonPDF.frame = CGRectMake(SCREEN_WIDTH/2 - 100, 100, 200, 36);
[buttonPDF setImage:imageButton forState:UIControlStateNormal];
buttonPDF.contentMode = UIViewContentModeScaleAspectFit;
buttonPDF.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
[buttonPDF addTarget:self action:#selector(displayPDFParams:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonPDF];
}
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = documentParametres;
[self.navigationController pushViewController:pdfController animated:YES];
}
displayPDFParams is called but it not push me on my pdfController. I think it's because I'm not able to target the parent navigation controller of my application...
Anyone help would be much appreciated. Thanks in advance !
You need To initialize your root view controller with the navigation controller. Here is the code.
In your AppDelegate.h
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
#class HomeViewController;
#interface IDSAppDelegate : UIResponder <UIApplicationDelegate>{
UINavigationController *nav;
}
#property (strong, nonatomic) HomeViewController *homeViewController;
#end
In your AppDelegate.m
#import "IDSAppDelegate.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
nav=[[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Problem solved by define a property in my UIViewController (as UITabBarItem) like this :
#property (nonatomic, retain) UINavigationController *superNavController;
And set it in my UITabBarController :
self.myViewController.superNavController = self.navigationController;
Finally I modified my displayPDFParams method :
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = self.documentParametres;
[self.superNavController pushViewController:pdfController animated:YES];
}
Works perfectly !
I have created a simple hello world project for cocos2d-x 3.2. Added AdMob banner. The essence of adding banner is to create a view, then add into that view first the cocos2d-x content and then banner content:
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self addChildViewController:_contentController];
[contentView addSubview:_contentController.view];
[_contentController didMoveToParentViewController:self];
[contentView addSubview:_bannerView];
self.view = contentView;
The result is that on iPhone, iPhone Retina, iPad Retina (but not in iPad) the Hello World text is distorted like this:
At the same time that stats text is not distorted:
I cannot understand what happens and why. Here is my full code:
.hfile
#import <UIKit/UIKit.h>
#interface BannerViewController : UIViewController
- (instancetype)initWithContentViewController:(UIViewController *)contentController;
- (void) hideBanner;
- (void) showBanner;
#end
and hers is .mm file
#import "BannerViewController.h"
#import "GADBannerView.h"
#interface BannerViewController () <GADBannerViewDelegate>
#end
#implementation BannerViewController {
GADBannerView *_bannerView;
UIViewController *_contentController;
Boolean _bannerLoaded;
}
- (instancetype)initWithContentViewController:(UIViewController *)contentController
{
self = [super init];
if (self != nil) {
// use kGADAdSizeBanner for a small banner in iPad
_bannerView = [[GADBannerView alloc] initWithAdSize: kGADAdSizeSmartBannerPortrait]; // scaled banner dependent on device size
_bannerView.adUnitID = #"ca-app-pub-874958723945898/8247587858";
_bannerView.delegate = self;
_contentController = contentController;
_bannerLoaded = NO;
}
return self;
}
- (void)loadView
{
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self addChildViewController:_contentController];
[contentView addSubview:_contentController.view];
[_contentController didMoveToParentViewController:self];
[contentView addSubview:_bannerView];
self.view = contentView;
}
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [_contentController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#endif
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [_contentController preferredInterfaceOrientationForPresentation];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [_contentController supportedInterfaceOrientations];
}
// For animation
- (void)viewDidLayoutSubviews
{
CGRect contentFrame = self.view.bounds;
CGRect bannerFrame = CGRectZero;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
bannerFrame = _bannerView.frame;
#else
bannerFrame.size = [_bannerView sizeThatFits:contentFrame.size];
#endif
bannerFrame.origin.x = (contentFrame.size.width - bannerFrame.size.width) / 2;
if (_bannerLoaded) {
//contentFrame.size.height -= bannerFrame.size.height;
bannerFrame.origin.y = contentFrame.size.height - bannerFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
_contentController.view.frame = contentFrame;
_bannerView.frame = bannerFrame;
}
- (void)viewDidLoad {
[super viewDidLoad];
_bannerView.rootViewController = self;
[self.view addSubview:_bannerView];
GADRequest *request = [GADRequest request];
[_bannerView loadRequest:request];
}
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView
{
NSLog(#"adViewDidReceiveAd");
_bannerLoaded = YES;
[UIView animateWithDuration:0.25 animations:^{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];
}
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(#"adView didFailToReceiveAdWithError");
_bannerLoaded = NO;
[UIView animateWithDuration:0.25 animations:^{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];
}
- (void) hideBanner{
//TODO:
}
- (void) showBanner{
//TODO:
}
- (void)dealloc {
_bannerView.delegate = nil;
[_bannerView release];
[super dealloc];
}
#end
// </GADBannerViewDelegate>
And I use it in AppController.mm like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
// Init the CCEAGLView
CCEAGLView *eaglView = [CCEAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH24_STENCIL8_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
// Use RootViewController manage CCEAGLView
_viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
_viewController.wantsFullScreenLayout = YES;
_viewController.view = eaglView;
_bannerViewController = [[BannerViewController alloc] initWithContentViewController:_viewController];
// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[window addSubview: _bannerViewController.view];
//[window addSubview: _viewController.view];
}
else
{
// use this method on ios6
[window setRootViewController:_bannerViewController];
// [window setRootViewController:_viewController];
}
[window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden:true];
// IMPORTANT: Setting the GLView should be done after creating the RootViewController
cocos2d::GLView *glview = cocos2d::GLView::createWithEAGLView(eaglView);
cocos2d::Director::getInstance()->setOpenGLView(glview);
cocos2d::Application::getInstance()->run();
return YES;
}
What is wrong here?
I have found the problem. I appears that there is a time in hours written with black color like in the image, which on on the Hello World label and hence it distorts:
The time is shown is simulators status bar. In order to hide it I added this:
//fix not hide status on ios7
- (BOOL)prefersStatusBarHidden
{
return YES;
}
into BannerViewController class and that's it!
I have a MPMoviePlayerController that is initialized as follows:
//Code in my UIViewController
#property (nonatomic, strong) UIView *myVideoView;
#property (nonatomic, strong) MPMoviePlayerController *myVideoPlayer;
- (void) initializeVideoPlayer
{
CGRect frame = CGRectMake(0, 70, self.view.frame.size.width, 200);
self.myVideoView = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:self.myVideoView];
NSURL *videoURL = [NSURL fileURLWithPath:path];
self.myVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
self.myVideoPlayer.controlStyle = MPMovieControlStyleEmbedded;
self.myVideoPlayer.shouldAutoplay = YES;
[self.myVideoPlayer.view setFrame: self.myVideoView.bounds];
[self.myVideoView addSubview: self.myVideoPlayer.view];
//Play video
[self.myVideoPlayer prepareToPlay];
[self.myVideoPlayer play];
}
My question is how do I get the video to play in fullscreen when the user rotates the phone to landscape and not fullscreen when the phone is in portrait.
I've tried adding the following to my UIViewController
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
[self.myVideoPlayer setFullscreen:YES animated:YES];
}
else
{
[self.myVideoPlayer setFullscreen:NO animated:YES];
}
}
However, the problem with this is that once the player is in fullscreen, the willAnimateRotationToInterfaceOrientation no longer gets called; therefore, even when the user rotates back to portrait, the video is still in fullscreen.
Try using an MPMoviePlayerViewController instead of an MPMoviePlayerController. Just initialize it in your UIViewController and use its moviePlayer property as you would with a plain MPMoviePlayerController. If you subclass MPMoviePlayerViewController you can control what's happening when the device rotates by implementing willAnimateRotationToInterfaceOrientation etc.
In AppDelegate.h:
#property(nonatomic)BOOL allowRotation;
in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController * root = [[RootViewController alloc] init];
self.window.rootViewController = root;
//add two Notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MPVisionVideoNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MPVisionVideoNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
self.allowRotation = YES;
}
- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
self.allowRotation = NO;
}
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskLandscapeRight ;
}
return UIInterfaceOrientationMaskPortrait;
}
//this can rotate the windows when to fullscreen state
This is what I have so far:
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
-(void)animate
{
[UIView animateWithDuration:1 animations:^{
splashImage.frame = CGRectMake(0,480,320,480);
}];
[UIView commitAnimations];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
splashImage = [[UIImageView alloc] initWithImage:[UIImage
imageNamed:#"carolinaWolf.png"]];
splashImage.frame = CGRectMake(0, 0, 320, 480);
[self.tabBarController.view addSubview:splashImage];
[self animate];
return YES;
}
My initial view in storyboard is a TabBarController. What I want to happen is: after I finish loading new content into the application, I want to splash screen to animate down and off the screen. This worked before I started using storyboard, why won't it work now?
My AppDelegate.h looks like this:
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) UITabBarController *tabBarController;
#end
UIImageView *splashImage;
You can directly just add your splashImage to self.window.rootViewController like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIImage *splashImage = [UIImage autoAdjustImageNamed:#"Default.png"];
UIImageView *splashImageView = [[UIImageView alloc] initWithImage:splashImage];
[self.window.rootViewController.view addSubview:splashImageView];
[self.window.rootViewController.view bringSubviewToFront:splashImageView];
[UIView animateWithDuration:1.5f
delay:2.0f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
splashImageView.alpha = .0f;
CGFloat x = -60.0f;
CGFloat y = -120.0f;
splashImageView.frame = CGRectMake(x,
y,
splashImageView.frame.size.width-2*x,
splashImageView.frame.size.height-2*y);
} completion:^(BOOL finished){
if (finished) {
[splashImageView removeFromSuperview];
}
}];
return YES;
}
This is how I fade away my splash screens:
#pragma mark Fading Default.png Animation
#interface FadeDefault : UIViewController
#end
#implementation FadeDefault
- (void)viewDidLoad
{
[super viewDidLoad];
UIImageView *fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default#2x.png"]];
[fader setFrame:CGRectMake(0,0,320,480)];
/*if ( IDIOM == IPAD ) /* Optional */
{
fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default-Portrait~ipad.png"]];
[fader setFrame:CGRectMake(0,0,768,1024)];
}else
{
fader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Default#2x.png"]];
[fader setFrame:CGRectMake(0,0,320,480)];
}*/
self.view = fader;
[fader release];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(imageDidFadeOut:finished:context:)];
self.view.alpha = 0.0;
[UIView commitAnimations];
}
- (void)imageDidFadeOut:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context{
[self.view removeFromSuperview];
}
#end
#pragma mark - Application lifecycle
Keep in mind this is an example, although code I actually use and I know works, you need to keep your other didFinishLaunchingWithOptions code in place. Just add the fader code to you existing code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* custom fading Default.png animation */
FadeDefault * fader = [[FadeDefault alloc] init];
[window addSubview:navigationController.view];
[window addSubview:fader.view];
[fader release];
[window makeKeyAndVisible];
return YES;
}