Handle all rotations for dynamic view in iOS - ios

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];

Related

How to create custom slide menu without third party library.?

I want to implement slide menu in my iOS app like drawer (Andriod). I went through a tutorial, but all of them are using third party libraries. Is there any possibility to create a custom slide menu. I tried to create it with the following code, but it's only working with xib file:
- (IBAction)sidemenu:(id)sender
{
[UIView animateWithDuration:0.50f animations:^{
view.frame = self.view.frame;
} completion:^(BOOL finished) {
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipGestureLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
}];
}
- (void)SwipGestureAction
{
UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipGestureRightAction:)];
swiperight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];
}
#pragma mark AddSwipeGestureLeftAndRight
- (void)SwipGestureRightAction:(UISwipeGestureRecognizer *)swipeRight
{
[UIView animateWithDuration:0.50f animations:^{
view.frame = self.view.frame;
} completion:^(BOOL finished) {
swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(SwipGestureLeftAction:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
}];
}
- (void)SwipGestureLeftAction:(UISwipeGestureRecognizer *)swipeRight
{
[UIView animateWithDuration:0.50f animations:^{
[view setFrame:CGRectMake(self.view.frame.origin.x - self.view.frame.size.width, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height)];
} completion:^(BOOL finished){
[self.view removeGestureRecognizer:swipeLeft];
}];
}
Here is what I have for you:
I made a super class for all my slide menus in all projects. It manages the showing and hiding of the slide menu, and handles orientation changes. It slides in from left on top of the current view, and it partially obscures the remainder of the view with a dark transparent background.
If you ever need other behaviour (like pushing out the current view) just override the animation part.
My slide menu is a Singleton because in our applications we only use one slide menu on every screen.
#import <UIKit/UIKit.h>
#interface IS_SlideMenu_View : UIView <UIGestureRecognizerDelegate>
{
UIView* transparentBgView;
BOOL hidden;
int lastOrientation;
}
#property (strong, nonatomic) UIView *menuContainerV;
+ (id)sharedInstance;
- (BOOL)isShown;
- (void)hideSlideMenu;
- (void)showSlideMenu;
#end
#import "IS_SlideMenu_View.h"
#implementation IS_SlideMenu_View
+ (id)sharedInstance
{
static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[[self class] alloc] init];
});
return _sharedInstance;
}
- (instancetype)initWithFrame:(CGRect)frame
{
frame = [[[UIApplication sharedApplication] delegate] window].frame;
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
transparentBgView = [[UIView alloc] initWithFrame:frame];
[transparentBgView setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]];
[transparentBgView setAlpha:0];
transparentBgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognized:)];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(gestureRecognized:)];
[transparentBgView addGestureRecognizer:tap];
[transparentBgView addGestureRecognizer:pan];
[self addSubview:transparentBgView];
frame.size.width = 280;
self.menuContainerV = [[UIView alloc] initWithFrame:frame];
CALayer *l = self.menuContainerV.layer;
l.shadowColor = [UIColor blackColor].CGColor;
l.shadowOffset = CGSizeMake(10, 0);
l.shadowOpacity = 1;
l.masksToBounds = NO;
l.shadowRadius = 10;
self.menuContainerV.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self addSubview: self.menuContainerV];
hidden = YES;
}
//----- SETUP DEVICE ORIENTATION CHANGE NOTIFICATION -----
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
lastOrientation = [[UIDevice currentDevice] orientation];
return self;
}
//********** ORIENTATION CHANGED **********
- (void)orientationChanged:(NSNotification *)note
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
NSLog(#"%ld",orientation);
if(!hidden && lastOrientation != orientation){
[self hideSlideMenu];
hidden = YES;
lastOrientation = orientation;
}
}
}
- (void)showSlideMenu {
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
self.frame = CGRectMake(0, 0, window.frame.size.width, window.frame.size.height);
[self.menuContainerV setTransform:CGAffineTransformMakeTranslation(-window.frame.size.width, 0)];
[window addSubview:self];
// [[UIApplication sharedApplication] setStatusBarHidden:YES];
[UIView animateWithDuration:0.5 animations:^{
[self.menuContainerV setTransform:CGAffineTransformIdentity];
[transparentBgView setAlpha:1];
} completion:^(BOOL finished) {
NSLog(#"Show complete!");
hidden = NO;
}];
}
- (void)gestureRecognized:(UIGestureRecognizer *)recognizer
{
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]]) {
[self hideSlideMenu];
} else if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
static CGFloat startX;
if (recognizer.state == UIGestureRecognizerStateBegan) {
startX = [recognizer locationInView:self.window].x;
} else
if (recognizer.state == UIGestureRecognizerStateChanged) {
CGFloat touchLocX = [recognizer locationInView:self.window].x;
if (touchLocX < startX) {
[self.menuContainerV setTransform:CGAffineTransformMakeTranslation(touchLocX - startX, 0)];
}
} else
if (recognizer.state == UIGestureRecognizerStateEnded) {
[self hideSlideMenu];
}
}
}
- (void)hideSlideMenu
{
UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
window.backgroundColor = [UIColor clearColor];
[UIView animateWithDuration:0.5 animations:^{
[self.menuContainerV setTransform:CGAffineTransformMakeTranslation(-self.window.frame.size.width, 0)];
[transparentBgView setAlpha:0];
} completion:^(BOOL finished) {
[self removeFromSuperview];
[self.menuContainerV setTransform:CGAffineTransformIdentity];
// [[UIApplication sharedApplication] setStatusBarHidden:NO];
hidden = YES;
NSLog(#"Hide complete!");
}];
}
- (BOOL)isShown
{
return !hidden;
}
#end
Subclasses only need to add subviews to the menuContainerV view, and manage them.
An example:
I created a subclass that has an header view and a table view as its content. I created the content view in a xib, and the owner of the xib is this subclass. This way I can bind outlets to the xib.
#import "IS_SlideMenu_View.h"
#interface CC_SlideMenu_View : IS_SlideMenu_View<UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UIView *headerView;
#property (weak, nonatomic) IBOutlet UITableView *tableView;
...
#end
When the slide menu gets instantiated I load the xib and add the content view to the menuContainerV view.
#import "CC_SlideMenu_View.h"
#implementation CC_SlideMenu_View
- (instancetype)init
{
self = [super init];
if (self) {
UIView *v = [[[NSBundle mainBundle] loadNibNamed:#"CC_SlideMenu_View" owner:self options:nil] firstObject];
v.frame = self.menuContainerV.bounds;
[self.menuContainerV addSubview:v];
self.tableView.backgroundColor = [UIColor darkGrayColor];
}
return self;
}
...
#end
The result is something like this.

Why is the UIImageView not hiding when using a UISegmentedControl?

I have a UISegmentedControl where the first control will display the text within a UITextView while the second control displays a scrollable UIImageView.
In the initial startup, if I switch to the second control, the image displays and switching back to the first control, the image disappears and the UITextView shows.
However, when I switch to the second control the 2nd time and switch back to the first control, the image is still there and I cannot get the UITextView to show anymore.
My code has it to where the image is hidden and the text is shown in the first control, and vice versa in the second control.
Why was it working the first time, but then not working the second time I switched between controls?
What am I doing wrong?
Thanks
-(void)viewDidLoad
{
self.scrollView.delegate = self;
self.textView.text = #"THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST.";
self.textView.hidden = NO;
}
-(void)setScroller
{
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"] ];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
//self.scrollView.zoomScale = 1.0;
[self.scrollView addSubview:self.imageView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (IBAction)segmentedControl:(UISegmentedControl *)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0)
{
// Display apppropriate info for About
self.imageView.hidden = YES;
self.textView.hidden = NO;
}
else
{
self.imageView.hidden = NO;
self.textView.hidden = YES;
[self setScroller];
}
}
You should remove [self setScroller]; from the - (IBAction)segmentedControl:(UISegmentedControl *)sender method, and put it in -(void)viewDidLoad instead. You're calling [self setScroller]; every time you switch to the second segment.
Your code should look like:
-(void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
[self setupScroller];
}
-(void)setupScroller
{
// Set contentSize
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
self.scrollView.contentSize = scrollableSize;
// Add textView
self.textView.text = #"THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST. THIS IS A TEST.";
self.textView.hidden = NO;
// Add ImageView
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.imageView.hidden = YES;
[self.scrollView addSubview:self.imageView];
// Configure Zoom Scales and backgroundColor
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (IBAction)segmentedControl:(UISegmentedControl *)sender
{
if (self.segmentedControl.selectedSegmentIndex == 0)
{
// Display appropriate info for About
self.imageView.hidden = YES;
self.textView.hidden = NO;
}
else
{
self.imageView.hidden = NO;
self.textView.hidden = YES;
}
}
Your problem probably is that you creating a lot of instances of "imageView"
To solve your problem I suggest:
-(void)setScroller
{
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
if(self.imageView == nil) {
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
[self.scrollView addSubview:self.imageView];
}
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
//self.scrollView.zoomScale = 1.0;
[self.imageView setNeedsDisplay];
[self.imageView setImage:[UIImage imageNamed: #"test.png"]];
}
I've put only the Alloc of the self.imageView + the addSubView to the if,
All the rest are out side,
Now it will work

Set image to background image for view controller and maintain when rotated on ipad

I am trying to place an image as a background for viewcontroller in an ipad application. I set this using
UIImage *background = [UIImage imageNamed: #"Pilot#2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
This works fine for portrait mode, but when I rotate it to landscape mode there's a blank space where the picture doesn't fill. My idea based on other similar posts was to have a new picture that is called when the rotation get's changed and I tried this
{ if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
if((self.interfaceOrientation == UIDeviceOrientationLandscapeLeft) || (self.interfaceOrientation == UIDeviceOrientationLandscapeRight)){
UIImage *background = [UIImage imageNamed: #"Pilot#2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
} else if((self.interfaceOrientation == UIDeviceOrientationPortrait) || (self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)){
UIImage *background = [UIImage imageNamed: #"PilotLandscape#2x~ipad.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}}
Which resulted in the same thing. Any ideas on how to fix this would be appreciated!
Check view's autosizing settings, try to NSLog view's coordinates, pay attention to uiimageview's UIViewContentModeScale property. Why don't you simply add imageview to your storyboard?
Working Code :
#interface SOViewController (){
UIImageView *imageView;
}
#end
-(void)initialOrientationHandling
{
//[self.view removeFromSuperview];
if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeLeft ||
[[UIDevice currentDevice]orientation] == UIDeviceOrientationLandscapeRight){
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}else if ([[UIDevice currentDevice]orientation] == UIDeviceOrientationPortrait ||
[[UIDevice currentDevice]orientation] == UIDeviceOrientationPortraitUpsideDown){
imageView.frame = self.view.bounds;
[self.view addSubview: imageView];
[self.view sendSubviewToBack:imageView];
}
else if([[UIDevice currentDevice]orientation]== UIDeviceOrientationFaceUp){
if(self.view.frame.size.height < 900){
}else{
}
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self initialOrientationHandling];
}
Obviously the image will be distorted if not highly pixled. I recommand you use different for different rotations.
SWIFT:
First create two image, one is landscape view size and another for portrait.
Initialize imageview
var Imgview = UIImageView()
In viewWillAppear method , it change image when view load and also when rotate.
override func viewWillAppear(animated: Bool) {
Imgview.frame = self.view.frame
Imgview.contentMode = UIViewContentMode.ScaleAspectFill
Imgview.clipsToBounds = true
view.addSubview(Imgview)
// get notification while rotating
NSNotificationCenter.defaultCenter().addObserver(self, selector: "rotate", name: UIDeviceOrientationDidChangeNotification, object: nil
)
}
Function rotate for set image,it is called in notification center.
func rotate()
{
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{
Imgview.frame = self.view.frame
Imgview.image = UIImage(named: "landscapeSizeimage.png")
self.view.sendSubviewToBack(Imgview)
}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{
Imgview.frame = self.view.frame
Imgview.image = UIImage(named: "PortraitSizeimage.png")
self.view.sendSubviewToBack(Imgview)
}}
This worked for me.Hope it helps someone.And i dont know may be it create more imageviews so instead of bringtosubview we can remove imageview as your wish .

Popover with picker views not showing correctly

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;

Best practice to calculate view size within loadView method

What is the best practice to calculate the view size in the loadView method (in an UIViewController) without a XIB file?
Here is my solution:
- (void)loadView {
//Calculate Screensize
BOOL statusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden ];
BOOL navigationBarHidden = [self.navigationController isNavigationBarHidden];
BOOL tabBarHidden = [self.tabBarController.tabBar isHidden];
CGRect frame = [[UIScreen mainScreen] bounds];
if (!statusBarHidden) {
frame.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height;
}
if (!navigationBarHidden) {
frame.size.height -= self.navigationController.navigationBar.frame.size.height;
}
if (!tabBarHidden) {
frame.size.height -= self.tabBarController.tabBar.frame.size.height;
}
UIView *v = [[UIView alloc] initWithFrame: frame];
[v setBackgroundColor: [UIColor whiteColor] ];
[v setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
[self setView: v ];
[v release];
}
Is this code okay, or should I edit something?
The docs recommend using [[UIScreen mainScreen] applicationFrame] to get the screen bounds without the status bar
so for anyone who want to know a best practice example:
#pragma mark -
#pragma mark LoadView Methods
- (void)loadView {
//Calculate Screensize
BOOL statusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden ];
BOOL navigationBarHidden = [self.navigationController isNavigationBarHidden];
BOOL tabBarHidden = [self.tabBarController.tabBar isHidden];
BOOL toolBarHidden = [self.navigationController isToolbarHidden];
CGRect frame = [[UIScreen mainScreen] applicationFrame];
//check if you should rotate the view, e.g. change width and height of the frame
BOOL rotate = NO;
if ( UIInterfaceOrientationIsLandscape( [UIApplication sharedApplication].statusBarOrientation ) ) {
if (frame.size.width < frame.size.height) {
rotate = YES;
}
}
if ( UIInterfaceOrientationIsPortrait( [UIApplication sharedApplication].statusBarOrientation ) ) {
if (frame.size.width > frame.size.height) {
rotate = YES;
}
}
if (rotate) {
CGFloat tmp = frame.size.height;
frame.size.height = frame.size.width;
frame.size.width = tmp;
}
if (statusBarHidden) {
frame.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height;
}
if (!navigationBarHidden) {
frame.size.height -= self.navigationController.navigationBar.frame.size.height;
}
if (!tabBarHidden) {
frame.size.height -= self.tabBarController.tabBar.frame.size.height;
}
if (!toolBarHidden) {
frame.size.height -= self.navigationController.toolbar.frame.size.height;
}
UIView *v = [[UIView alloc] initWithFrame: frame];
v.backgroundColor = [UIColor whiteColor];
v.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view = v;
[v release]; //depends on your ARC configuration
}
You are adjusting the height depend on the status bar and navigation bars.. But you have not done anything with respect to the origin of the view.

Resources