I've a UIView under a table.
I've got to set UIView height for iPhone 4S or 5.
I tried using
- (void) regolaViewPeriPhone {
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
CGRect frame = CGRectMake(0, 210, 320, 245);
_pickerContainerView.frame = frame;
}
if(result.height == 568)
{
CGRect frame = CGRectMake(0, 210, 320, 290);
_pickerContainerView.frame = frame;
}
}
}
but it doesn't work, what's right method? Thank you!
[ [ UIScreen mainScreen ] applicationFrame ]
This is the way to "Set UIView height depending on screen size"
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
//Do u r stuff here for Iphone 5
}
else if(screenBounds.size.height==480)
{
//Do u r stuff here for Iphone 4s
}
//if my answer is correct then vote up for me...
Related
In my application, I use a custom pod called MMDrawerController, to create a dummy status bar, unfortunately in the pod the status bar's height is always set to 20.
In order to fix this issue I have written the following code:
App delegate
MMDrawerController *mmdrawer = [[MMDrawerController alloc]init];
//UPDATE IPHONE X STATUS BAR
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height == 812.0f) {
NSLog(#"DEVICE NAME : iPhone X");
self.iphoneXHeight = -45.0;
self.iphoneXHeightPos = 45.0;
mmdrawer.height = 40;
}
else {
self.iphoneXHeight = -20.0;
self.iphoneXHeightPos = 20.0;
mmdrawer.height = 20;
}
}
MMDrawerController.h
#property (nonatomic,assign) CGFloat height;
MMDrawerController.m
_dummyStatusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), self.height)];
Problem:
When I run my code the height property is always 0, would appreciate it if someone can point out what I am doing wrong here and how would I be able access the height property and modify it ?
Dont use fixed height for the status bar, you can get the height with this code:
UIApplication.sharedApplication.statusBarFrame.size.height
Change this line.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if (screenSize.height == 812.0f) {
NSLog(#"DEVICE NAME : iPhone X");
self.iphoneXHeight = -45.0;
self.iphoneXHeightPos = 45.0;
mmdrawer.height = 40;
}
With this one:
if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
// determine screen size
int screenHeight = [UIScreen mainScreen].bounds.size.height;
switch (screenHeight) {
// iPhone 5s
case 568:
NSLog(#"iPhone 5 or 5S or 5C");
break;
// iPhone 6
case 667:
NSLog(#"iPhone 6/6S/7/8");
break;
// iPhone 6 Plus
case 736:
NSLog(#"iPhone 6+/6S+/7+/8+");
break;
// iPhone X
case 814:
NSLog(#"iPhone X");
break;
default:
// it's an iPad
printf("unknown");
}
}
I have created a custom keyboard and also it supports landscape and portrait orientation.My issue is when i change the orientation keyboard view changes its height and width which happens not like native keyboard.video is added below.I want keyboard to adjust is size like native keyboard.
I am updating keyboard height in viewWillTransitionTSize added below
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[self updateKeyboardHeight];
}
-(void)updateKeyboardHeight{
if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0)
return;
[self.inputView removeConstraint:self.heightConstraint];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat realScreenHeight = MAX(screenSize.height, screenSize.width);
if([[UIScreen mainScreen] bounds].size.width == realScreenHeight)
{
//Landscape
self.isLandscape = YES;
self.heightConstraint.constant = self.landscapeHeight;
_currentOrientation = LKeyboardButtonLandscape;
[self.view addConstraint:self.heightConstraint];
}
else
{
//Portrait
self.isLandscape = NO;
self.heightConstraint.constant = self.portraitHeight;
_currentOrientation = LKeyboardButtonPortrait;
[self.view addConstraint:self.heightConstraint];
}
}
I have used this method for detecting the width and height of screen. But its showing the width as 768 and height as 1024 in portrait and landscape also.
CGRect screenBounds = [[UIScreen mainScreen] bounds];
float widthfloat= screenBounds.size.width;
float heightfloat= screenBounds.size.height;
NSLog(#" width float %f",widthfloat);
NSLog(#"height float %f",heightfloat);
NSLog(#"width view %f \n height view %f", self.view.bounds.size.width, self.view.bounds.size.height);
NSLog(#"width %f \n height %f", screenBounds.size.width, screenBounds.size.height);
float wvalue = [[UIScreen mainScreen] bounds].size.width;
float hvalue = [[UIScreen mainScreen] bounds].size.height;
NSLog(#" wvalue %f",wvalue);
NSLog(#"hvalue %f",hvalue);
CGFloat width1 = [UIScreen mainScreen].bounds.size.width;
CGFloat height1 = [UIScreen mainScreen].bounds.size.height;
NSLog(#"width1 %f",width1);
NSLog(#"height1 %f",height1);
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGRect screenBounds1 = [[UIScreen mainScreen] bounds];
CGSize screenSize = CGSizeMake(screenBounds1.size.width * screenScale, screenBounds1.size.height * screenScale); if (screenSize.height==1136.000000)
NSLog(#"abcd1 %f",screenBounds1.size.width);
NSLog(#"abcd2 %f",screenBounds1.size.height);
NSLog(#"efgh1 %f",screenSize.width);
NSLog(#"efgh2 %f",screenSize.height);
bounds contains the bounding rectangle of the screen, measured in points. It does not care about screen orientation.
You can calculate the screen size, I do something like this for me -
-(CGSize)screenSize
{
CGSize size = [UIScreen mainScreen].applicationFrame.size;
if(UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
return size;
else
{
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
if (size.height > 480.0)
// This is iPhone-5
size = CGSizeMake(568.0, 320.0);
else
// This is iPhone-4
size = CGSizeMake(480.0, 320.0);
}
else
{
// This is iPad
size = CGSizeMake(1024.0, 768.0);
}
if (![[UIApplication sharedApplication] isStatusBarHidden])
size.height -= 20.0;
return size;
}
}
Note: The method will give you the height after reducing the height of the statusbar if available.
I want to increase the size of a custom button if the user is using an iPhone 5.
This is what I have in my .m file
//.m File
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
int varWidth = 228;
}
if(result.height == 568)
{
int varWidth = 272;
}
}
....
[newButton setFrame:CGRectMake(8.0, 40.0, 228, 80.0)];
But I want something like this:
[newButton setFrame:CGRectMake(8.0, 40.0, varWidth, 80.0)];
You are using varWidth out of it's scope.
int varWidth;
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
varWidth = 228;
}
if(result.height == 568)
{
varWidth = 272;
}
}
....
[newButton setFrame:CGRectMake(8.0, 40.0, varWidth, 80.0)];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[newButton setFrame:CGRectMake(8.0, 40.0, 228.0, 80.0)];
}
if(result.height == 568)
{
[newButton setFrame:CGRectMake(8.0, 40.0, 272, 80.0)];
}
}
Why not do this way?
Another suggestion:
Use #define.
For example:
#define iPhone5width 272
#define iPhone4width 228
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
[newButton setFrame:CGRectMake(8.0, 40.0, iPhone4width, 80.0)];
}
if(result.height == 568)
{
[newButton setFrame:CGRectMake(8.0, 40.0, iPhone5width, 80.0)];
}
}
The Best and Short way, For check device is iPhone 5 or iPhone5 < (Less Then).
For get this you need to write following code in your .pch file of project.
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
This Like check device, is it iPhone5 or not.
And you need to write only one condition for manage it
if( IS_IPHONE_5 )
// set or put code related to iPhone 5.
else
// set or put code related to less then iPhone 5.
I have taken a universal project. I have coded for iPhone 5 screen but it doesn't detect it.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
//ui for ipad
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568) {
tableView.frame = CGRectMake(0, 40, 320 ,508);
}
else {
tableView.frame = CGRectMake(0, 40, 320 ,420);
}
}
It doesn't work though. When I debug the code on iPhone 5 screenBounds = 320*480.
Where am I going wrong?
It is so simple to detect iPhone5 screen,
Just write
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
in your .pch file.... and you can further check for iPhone5 screen like if(IS_IPHONE5).
Choose your UITableView from the IB and select the size as Retina 4 for all screens, even the main window xib file. This works fine even on the iphone 4.
you add Splash Screen for iphone 5.Default-568h#2x.png file then its work fine
This code works fine for me
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
if (IS_IPHONE_5)
{
//iphone 5
}
Try using this to detect iPhone 5 instead:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
CGSize theHeight = [[UIScreen mainScreen] bounds].size;
if(theHeight.height == 568) {
// iPhone 5
tableView.frame = CGRectMake(0, 40, 320 ,508);
}
else {
// iPhone 4
tableView.frame = CGRectMake(0, 40, 320 ,420);
}
}
Alternatively you can define this:
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
Which you can re-use whenever you need to find out if it's iPhone 5 or not.
Try this code ::
if ([[UIScreen mainScreen] bounds].size.height == 568)
{
[tableView setFrame:CGRectMake(0, 40, 320 ,508)];
}
else if ([[UIScreen mainScreen] bounds].size.height == 480)
{
[tableView setFrame:CGRectMake(0, 40, 320 ,420)];
}
Hope, it'll help you.
Thanks.
I used this code to check iPhone 5 device.
float scrnheight = [[UIScreen mainScreen] bounds].size.height ;
if(scrnheight == 568.0f)
{
self.isIphone5 = true;
}