This is the code I have for when I try to open a popover that has 2 pickerviews in it.
-(void) showPopover {
NSLog(#"Showing popover.");
BOOL right = NO;
BOOL detected = NO;
int translate = 0;
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait) {
NSLog(#"Device is now in Portrait Mode");
translate += 600;
detected = YES;
}
else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) {
NSLog(#"Device is now in LandscapeLeft Mode ");
detected = YES;
}
else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) {
NSLog(#"Device is now in LandscapeRight Mode");
right = YES;
detected = YES;
}
else if([[UIDevice currentDevice]orientation] == UIDeviceOrientationPortraitUpsideDown) {
NSLog(#"Device is now in PortraitUpsideDown Mode");
detected = YES;
}
if(detected == NO) {
translate = 0;
NSLog(#"FREAK ACCIDENT, MATRIX GLITCH");
//right = YES;
}
if(right) translate += 600;
NSLog(#"Translate is %i", translate);
UIView *windowView = [[UIApplication sharedApplication] keyWindow];
UIViewController* popoverContent = [[UIViewController alloc]
init];
UIView* popoverView = [[UIView alloc]
initWithFrame:CGRectMake(0, 0, 600, 350)];
popoverView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.0];
[pickerView setFrame:CGRectMake(0, 0, 150, 216)];
[popoverView addSubview:pickerView];
[secondPickerView setFrame:CGRectMake(150, 0, 450, 216)];
[popoverView addSubview:secondPickerView];
popoverContent.view = popoverView;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(200, 250, 200, 50)];
[button setTitle:#"Go!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[popoverView addSubview:button];
popoverContent.contentSizeForViewInPopover =
CGSizeMake(600, 300);
popoverController = [[UIPopoverController alloc]
initWithContentViewController:popoverContent];
[popoverController presentPopoverFromRect:CGRectMake(70 + translate, 512, 1, 1)
inView:windowView
permittedArrowDirections:UIPopoverArrowDirectionDown
animated:YES];
}
And if my iPad is laying down, the "matrix glitch" error prints out and causes my popover to not function properly. It also sometimes happen if the device is in a portrait mode.
The top picture is what it looks like when my popover doesn't function properly, the bottom is it working correctly.
[[UIDevice currentDevice] orientation] gives you the physical orientation
of the device, and there are 7 possible values:
typedef enum {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,
UIDeviceOrientationPortraitUpsideDown,
UIDeviceOrientationLandscapeLeft,
UIDeviceOrientationLandscapeRight,
UIDeviceOrientationFaceUp,
UIDeviceOrientationFaceDown
} UIDeviceOrientation;
What you probably want is the current orientation of the interface, which you get
by calling the interfaceOrientation method of the current view controller.
It has the 4 possible values
typedef enum : NSInteger {
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;
Related
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];
}
I've created a runtime image view and added subviews to it (just like splash screen concept). Now i've to handle all 4 rotations for my iPad but what is the approach for it?
My code snippet:
UIView *mainScreen = [[[UIApplication sharedApplication]delegate]window];
UIView *windowBlocker = [[UIView alloc]initWithFrame:mainScreen.frame];
windowBlocker.tag = 999;
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(165, 200, 450, 480)];
imageView.layer.backgroundColor=[[UIColor colorWithRed:248/255 green:248/255 blue:248/255 alpha:0.5] CGColor];
imageView.alpha = 0.6;
imageView.layer.cornerRadius=10;
imageView.layer.masksToBounds = YES;
[windowBlocker addSubview:imageView];
This below approach would be painful to set each subview's co-ordinate for each rotation. isn't it?
How to handle the rotation here. I have to disable auto layout also for scrolling purpose.
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"Landscape left");
self.lblInfo.text = #"Landscape left";
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(#"Landscape right");
self.lblInfo.text = #"Landscape right";
} else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
NSLog(#"Portrait");
self.lblInfo.text = #"Portrait";
} else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
NSLog(#"Upside down");
self.lblInfo.text = #"Upside down";
}
}
pls guide.
Thanks
UIView *mainScreen = [[[UIApplication sharedApplication]delegate]window];
UIView *windowBlocker = [[UIView alloc]initWithFrame:mainScreen.frame];
windowBlocker.tag = 999;
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(165, 200, 450, 480)];
imageView.layer.backgroundColor=[[UIColor colorWithRed:248/255 green:248/255 blue:248/255 alpha:0.5] CGColor];
imageView.alpha = 0.6;
imageView.layer.cornerRadius=10;
imageView.layer.masksToBounds = YES;
[windowBlocker addSubview:imageView];
[[[mainScreen subviews] objectAtIndex:0] addSubview:windowBlocker];
I have used following code to display the iAD
bannerView = [[ADBannerView alloc]initWithFrame:
CGRectMake(0, 0, 320, 50)];
// Optional to set background color to clear color
bannerView.delegate=self;
[bannerView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview: bannerView];}
iAd not displayed in simulator.Every time to call following delegate method
-(void)bannerView:(ADBannerView *)bannerdidFailToReceiveAdWithError:(NSError *)error{
NSLog(#"Error loading %#",error);}
Hi You can follow the following steps for iAd
Add Delegate OF banner view
<ADBannerViewDelegate>
//Set Property
#property(nonatomic, strong) ADBannerView *bannerView;
viewDidLoad
//*************** ADBannerView ***********
bannerView = [[ADBannerView alloc]
init];
bannerView.delegate = self;
[bannerView setBackgroundColor:[UIColor clearColor]];
//***********************************************
//*********************** For iAd ***************************
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
bannerView.frame = CGRectMake(0, self.view.frame.size.height-66, 750, 80);
}
else
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending){
bannerView.frame = CGRectMake(0, self.view.frame.size.height-110, 320, 50);
}else{
bannerView.frame = CGRectMake(0, self.view.frame.size.height-50, 320, 50);
}
}else{
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending){
bannerView.frame = CGRectMake(0, self.view.frame.size.height-50, 320, 50);
}else{
bannerView.frame = CGRectMake(0, self.view.frame.size.height-50, 320, 50);
}
}
}
[self.view addSubview:bannerView];
[self.view bringSubviewToFront:bannerView];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave{
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
if ([[UIScreen mainScreen] bounds].size.height == 568) {}else{}
}
-(void)bannerView:(ADBannerView *)banner
didFailToReceiveAdWithError:(NSError *)error
{
}
Thanks
I am using ios 7 I want to set stauts bar background image.
I have done this but still it is not changing anything:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
addStatusBar.backgroundColor = [UIColor redColor]; //change this to match your navigation bar
[self.window.rootViewController.view addSubview:addStatusBar];
}
I have done this like .h file
#property (retain, nonatomic) UIWindow *statusBarBackground;
and in .m file
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
self.statusBarBackground = [[UIWindow alloc] initWithFrame: CGRectMake(0, 0, self.window.frame.size.width, 20)];
self.statusBarBackground.backgroundColor =[UIColor colorWithPatternImage:[UIImage imageNamed:#"statusbar_bg"]];
[self.statusBarBackground makeKeyAndVisible];
}
add this to your controllers
- (void) viewDidLayoutSubviews {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")) {
CGRect viewBounds = self.view.bounds;
if (viewBounds.origin.y == 0) {
CGFloat topBarOffset = self.topLayoutGuide.length;
viewBounds.origin.y -= topBarOffset;
self.view.bounds = viewBounds;
}
}
}
your code works, but you have to modify it a bit. here is what it should look like..
// Override point for customization after application launch.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
UIView *addStatusBar = [[UIView alloc] init];
addStatusBar.frame = CGRectMake(0, 0, 320, 20);
//change this to match your navigation bar or view color or tool bar
//You can also use addStatusBar.backgroundColor = [UIColor BlueColor]; or any other color
addStatusBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bg1.png"]];
//here you are adding the image as the background image
[self.window.rootViewController.view addSubview:addStatusBar];
don't forget to import your image to the project. now i have just plugged in the above code in application didfinishwithoptions of the app delegate, but you should be able to use the same if you want different views using the same.
You have to do 2 things.
(1) Open your info.plist and set "View controller-based status bar appearance" = NO
(2) add these lines to application:didFinishLaunchingWithOptions
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.window.clipsToBounds = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
self.window.frame = CGRectMake(20, 0,self.window.frame.size.width-20,self.window.frame.size.height);
self.window.bounds = CGRectMake(20, 0, self.window.frame.size.width, self.window.frame.size.height);
} else
{
self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
}
}
I have an app that I want to only work with in Landscape.
For the first time ever, I'm foregoing IB and trying to set up all my views programmatically, so I'm creating a view and adding a bunch of subviews in loadView method:
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:6];
self.mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
self.mapView.myLocationEnabled = YES;
self.mapView.delegate = self;
self.mapView.mapType = kGMSTypeHybrid;
self.mapView.frame = self.view.frame;
[self.view addSubview:self.mapView];
// add the toolbar
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
toolbar.barStyle = UIBarStyleDefault;
NSMutableArray* items = [NSMutableArray array];
[items addObject:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow.png"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(locateMe:)]];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:#"Tools"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(toolsButtonTapped:)]];
[toolbar setItems:items];
[self.view addSubview:toolbar];
In my project settings, I have disabled both portrait orientations. I also have this in my root view controller:
// Enforce Landscape Orientation
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
My problem is that the simulator starts in landscape mode, but all of the views are sized for portrait - so the bottom chunk of my views are below the screen and the right side of my screen is a big empty region.
I tried fixing this by switching the width and height of the application frame in the first line, but then that leaves some empty vertical room on the left edge of the screen for the status bar.
So, what's the correct way of doing what I'm trying to do?
Instead of using [[UIScreen mainScreen] applicationFrame]
try using [[[[[self view] window] rootViewController] view] bounds]
The bounds will represent the width and height correctly in Landscape orientation, because the bounds will take into account the transform (rotation) that has been applied, while the frame will not.
To see what I mean, set a breakpoint, and in the debugger print out the description of the top level view lldb> po [[[[self view] window] rootViewController] view]
You'll see that the view has a rotation transform and that its frame does not represent the dimensions of the screen in landscape, but represents the dimensions in portrait!
The long way to calculate the correct applicationFrame would be
BOOL iOS7 = NO;
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:#"7.0" options:NSNumericSearch] != NSOrderedAscending)
iOS7 = YES;
CGRect theFrame;
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
theFrame.origin = CGPointZero;
theFrame.size.width = screenBounds.size.height;
theFrame.size.height = screenBounds.size.width;
if (iOS7 == NO) {
// statusBarFrame will be CGRectZero if not visible, so this is safe
theFrame.size.height -= statusBarFrame.size.width; // because we're in landscape orientation
}
}
else {
theFrame = screenBounds;
if (iOS7 == NO) {
// statusBarFrame will be CGRectZero if not visible, so this is safe
theFrame.size.height -= statusBarFrame.size.height; // because we're in portrait orientation
}
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape( interfaceOrientation))
{
return YES;
}
return NO;
}
Put this code Appdelegate .M.....
Put this in current viewcontroller
// ios 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) {
return YES;
}
return NO;
}
// ios6
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}