I have an iPad project structured as:
- AppDelegate
- MainWindow
- View Controller
-- View
The View Controllers .m file loads another view programmatically and positions it on the screen. This view is going to be slid in and out.
I do this:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 0, 0);
CalculatorView *v = [[[CalculatorView alloc]
initWithFrame:viewRect] autorelease];
[self.view.window addSubview:v];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
v.view.frame = CGRectMake(0, 0, 460, 320);
[UIView commitAnimations];
}
The issue that I am having is that the subview I'm adding here doesnt' seem to have the correct orientation. The project supports ONLY landscape, and launches to landscape. The container view is fine, and it contains some buttons which are fine as well. However this programmatically loaded view is stuck in Portrait mode. I have provided the following auto-rotation code (In the loaded view's .m):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
But it never gets called.
So, how can I get the programmatically added subview to load in landscape and NOT portrait mode?
TIA!
The UIView class does not receive orientation change messages.
(specially the shouldAutorotateToInterfaceOrientation method, which is a UIViewController Method)
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW23
You will have to manually add a method in your view to inform it that the orientation has changed and you should call this method in your controller shouldAutorotateToInterfaceOrientation method.
To do that you will have to create a reference to you view in your controller and take care of the memory yourself.
#interface MyController : UIViewController {
CalculatorView *_calculatorView;
}
#end
#implementation MyController
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect viewRect = CGRectMake(0, 0, 0, 0);
//inits _calculatorView without the autorelease. Will be released in the dealloc method
_calculatorView = [[CalculatorView alloc]
initWithFrame:viewRect];
[self.view.window addSubview:v];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
_calculatorView.view.frame = CGRectMake(0, 0, 460, 320);
[UIView commitAnimations];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//calls custom interface orientation method
[_calculatorView MyInterfaceChangedCustomMethod:interfaceOrientation];
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(void) dealloc {
[_calculatorView release];
[super dealloc];
}
#end
EDIT: If you CalculatorView is simple and all you need is to change its frame properly after the device rotation, I think the best approach would be using you view's autoresizingMask similar to the following
_calculatorView = [[CalculatorView alloc]
initWithFrame:viewRect];
_calculatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
Related
I am trying to update my willAnimateRotationToInterfaceOrientation code to use the new viewWillTransitionToSize as a part of IOS 9.
In my app, I have two totally separate views for landscape and portrait (they are laid out differently, in a different pattern).
During rotation the self.view of the viewcontroller is assigned to the correct view (landscape or portrait). In IOS 9 using viewWillTransitionToSize, this revealed a timing race condition between the rotation animation and the assignment of the self.view.
The assignment of self.view = portraitView or self.view = landscapeView could happen either before the rotation, or after the rotation. It appears that because a view assignment is not an "animatable" property that it is executed at any time during the animateWithDuration window.
How do I force the view assignment to happen before the rotation animation?
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
UIInterfaceOrientation oldOrientation = [[UIApplication sharedApplication] statusBarOrientation];
UIInterfaceOrientation orientation = [Utilities orientationByTransforming:[coordinator targetTransform] fromOrientation:oldOrientation];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
[UIView animateWithDuration:[context transitionDuration] animations:^{
if (UIInterfaceOrientationIsLandscape(orientation)) {
self.view = self.landscapeView;
}
else{
self.view = self.portraitView;
}
} completion:^(BOOL finished){
//no completion logic for animateWithDuration
}];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
//no completion logic for animateAlongsideTransition
}];
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}
I think you need to have both views available:
[self.view addSubview: self.landscapeView];
[self.view addSubview: self.portraitView];
And then fade between them
self.landscapeView.alpha = self.portraitView.alpha = 0.0f;
[UIView animateWithDuration:[context transitionDuration] animations:^{
if (UIInterfaceOrientationIsLandscape(orientation)) {
self.landscapeView.alpha = 1.0f;
}
else{
self.portraitView.alpha = 1.0f;
}
I want to implement an iAd at the bottom of my screen. My app runs in landscape mode only, but the iAd is in portrait mode, its too big and I want it correct.
I instantiate it with this :
ADBannerView *adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, 480, 32)];
[self.view addSubview:adView];
So i give it 480 x 32 but it is in 360x 50.
That makes no sense to me and I don't know what to do. In the other topic I read I have to disable Auto Layout, but it is disabled.
thanks for any help!
edit: even if I add the iAd via storyboard to the view in landscape format, it shows it in portrait format when i run the app.
edit: I think I don't have a ADBannerView Appdelegate method.
All I have for my iAd is :
GameViewcontroller.h
#interface GameViewController : UIViewController<ADBannerViewDelegate>
GameViewcontroller.m
#interface GameViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake((self.view.frame.size.width /2) - 180, self.view.frame.size.height, 480, 32)];
_adBanner.delegate = self;
[self.view addSubview:_adBanner];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
// If banner isn't part of view hierarchy, add it
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, - banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError: (NSError *)error
{
NSLog(#"Failed to retrieve ad");
if (_bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}
I used the code below to present vViewController1
#property (retain,nonatomic) ViewController1 *vViewController1;
...
push vViewController1 from rootViewController
[self.navigationController pushViewController:vViewController1 animated:NO];
Viewcontroller1's viewWillAppear
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect frame = CGRectMake(0, 20, 320, 480);
self.view.frame = frame; ///aaa
NSLog(#"%f:%f:%f:%f",
frame.origin.x,frame.origin.y,frame.size.width,frame.size.height);
}
it outputs
0.000000:20.000000:320.000000:480.000000
but the view y position is always at
0
rather than
20
it looks like it is always
CGRectMake(0, 0, 320, 480);
and
self.view.frame = frame; ///aaa
can not relocate the position of the view.
I have try to set the position using code when call pushViewController,
but there is no way to make it work correctly.
Just wonder if it is a bug for pushViewController
Your comment welcome
I think you have autolayout turned on. So you need to override viewDidLayoutSubviews method like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.view.frame = CGRectMake(0, 0, 320, 480);
NSLog(#"%#", NSStringFromCGRect(self.view.frame));
}
And it's better to use NSStringFromCGRect for NSLog, rather creating 4 outputs doubles.
Hello I am trying to implement iAds at the bottom of my app When I change size to 3.5" screen, the iAd banner I have at the bottom of screen disappears. When I switch back to 4", the banner is back. How do I lock down the banner to display at bottom of screen regardless of screen size?
Here is my .h file
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#interface ViewController : UIViewController <ADBannerViewDelegate>
{
ADBannerView *adView;
BOOL bannerIsVisible;
NSInteger HighScoreNumber;
IBOutlet UILabel *HighScore;
}
#property (nonatomic,assign) BOOL bannerIsVisible;
#end
this is .m file
#synthesize bannerIsVisible;
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOn" context:NULL];
//banner is invisible
banner.frame = CGRectOffset(banner.frame, 0, 560);
[UIView commitAnimations];
self.bannerIsVisible = YES;
[banner setAlpha:1];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:#"animatedAdBannerOff" context:NULL];
//banner is visible and we moved it out of the screen due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -560);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication: (BOOL)willLeave
{
NSLog(#"Banner view is beginning an ad action");
BOOL shouldExecuteAction = YES;
if (!willLeave && shouldExecuteAction) {
// stop all interactive proccess in the app
// video pause
//audio pause
}
return shouldExecuteAction;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
// resume everything
// video
// audio
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:#"HighScoreSaved"];
HighScore.text = [NSString stringWithFormat:#"HighScore: %li", (long)HighScoreNumber];
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
[adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
}
- (void) viewWillDisappear:(BOOL)animated
{
[adView removeFromSuperview];
adView.delegate = nil;
adView = nil;
}
Here is all of my code for iAds I really need help with this issue.Thanks
I know this is old but I had this problem recently, and I found a solution so I will post it here in case someone else stumbles upon it.
I noticed that when you change the phones for some reason the ViewController retained the size specified in the storyboard (in my case it was iPhone 6).
So my fix was in the viewDidLoad method I got the size of the main screen after it loaded, instead of the size of the actual view or frame.
Swift:
var screenSize = UIScreen.mainScreen().bounds
println(screenSize)
var viewSize = self.view.bounds
println(viewSize)
Objective-C:
CGSize sizeOfScreen = [[UIScreen mainScreen] bounds].size;
CGSize sizeOfView = self.view.bounds.size;
and the corresponding output in their respective order:
(0.0, 0.0, 320.0, 568.0)
(0.0, 0.0, 375.0, 667.0)
I testing this on a iPhone5S which has a screen size of 320x568, but when asked for the view's bounds I got 375x667 which is the size of which I had made it in the storyboard.
Hope this helps someone!
Add a #define to know which device uses the App :
#define IS_IPHONE5() ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) && [UIScreen mainScreen].bounds.size.height == 568)
Next, use this #define to modify the offset :
banner.frame = CGRectOffset(banner.frame, 0, (IS_IPHONE5() ? 560.0f : 420.0f));
and
banner.frame = CGRectOffset(banner.frame, 0, (IS_IPHONE5() ? -560.0f : -420.0f));
Set the right value for iPhone 4" and 3.5"
Set the banner position at viewWillAppear where you can access the self.view.bounds.size, don't forget to add constraints or set autoResizingMask to make it "stick" to the bottom.
I am displaying an image programatically in my storyboard and handling successfully landscape/portrait orientation (see code below). I'm trying now to add a custom button on that same view in the storyboard via Interface Builder directly. The button just does not show up when the app runs. Only the image. Could you show me the way to do it on the storyboard view? Or should I add it programatically as well? Or redo everything 100% in Interface builder? Don't really know how to mix and match these 2 methods...
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
UIImage *startImage = [UIImage imageNamed:#"title.png"];
UIImageView *startImageView = [[UIImageView alloc] initWithImage:startImage];
self.startImageView = startImageView;
[self.view addSubview:startImageView];
}
- (void) orientStartImageView
{
UIInterfaceOrientation curOrientation = [UIApplication sharedApplication].statusBarOrientation;
if (curOrientation == UIInterfaceOrientationPortrait || curOrientation == UIInterfaceOrientationPortraitUpsideDown) {
[self.startImageView setFrame:CGRectMake(-128, 0, 1024, 1024)];
}else{
[self.startImageView setFrame:CGRectMake(0, -128, 1024, 1024)];
}
}
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self orientStartImageView];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self orientStartImageView];
}
Its bcoz you are adding ImageView on top of the Button, you can bring it to front using this code, put it in viewdidload method :
[self.view bringSubviewToFront:yourBtn];