Custom Keyboard iOS - Orientation change - Autolayout constraints applied not smoothly - ios

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

Related

Change status bar height, based on if the device is an iPhone X or not

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");
}
}

Im trying to detect which size of the phone the user got and set the scrollview length to appropriate size

Im trying to detect which size of the phone the user got and set the scrollview length to appropriate size. For example if the user is using an iPhone plus the scroll length should be shorter then if the user is using an iPhone 5
- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scroll2.frame.size.width;
int page = floor((self.scroll2.contentOffset.x - pageWidth / 3) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
if ([[UIScreen mainScreen] respondsToSelector:#selector(scale)]) {
if ([[UIScreen mainScreen] scale] == 2.0) {
if([UIScreen mainScreen].bounds.size.height == 667.0){
self.scroll2.contentSize = CGSizeMake(300, 100);
// iPhone retina-4.7 inch(iPhone 6)
}
else if([UIScreen mainScreen].bounds.size.height == 568.0){
self.scroll2.contentSize = CGSizeMake(300, 5500);
// iPhone retina-4 inch(iPhone 5 or 5s)
}
else{
// iPhone retina-5 inch inch(iPhone Plus)
}
}
}
You can achieve this auto layout.Just put you all content in a UIView and add this UIView inside scroll view with five constraint
Fix Height
Top
Bottom
Leading
Trailing
After that you can achieve the solution without headache of checking screen size. If you are new at auto layout than go through this tutorial https://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/

Change NSLayoutConstraint constant not working in layoutSubviews

I am trying to change a UIButton's width when the view animates to landscape mode. But the method is called because I set a break point there, but the button's width doesn't change. I add a IBOutlet constraint to button's width named: globalButtonWidthConstraint.
My current code :
- (void)layoutSubviews {
[super layoutSubviews];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat screenH = screenSize.height;
CGFloat screenW = screenSize.width;
BOOL isLandscape = !(self.frame.size.width == (screenW*(screenW<screenH))+(screenH*(screenW>screenH)));
if (isLandscape) {
self.globalButtonWidthConstraint.constant = 100;
[self layoutIfNeeded];
} else {
self.globalButtonWidthConstraint.constant = 47;
[self layoutIfNeeded];
}
}
Try updating constraint constant in "viewDidLayoutSubviews".
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Update constraint constant
[self.view layoutSubviews];
}
It worked for me.
EDIT: Make sure that there is no other constraints that conflicting this width constraint.

Set UIView height depending on screen size

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...

How to get orientation-dependent height and width of the screen?

I'm trying to programmatically determine the current height and width of my application. I use this:
CGRect screenRect = [[UIScreen mainScreen] bounds];
But this yields a width of 320 and a height of 480, regardless of whether the device is in portrait or landscape orientation. How can I determine the current width and height (i.e. dependent upon the device orientation) of my main screen?
You can use something like UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) to determine the orientation and then use the dimensions accordingly.
HOWEVER, during an orientation change like in UIViewController's
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
Use the orientation passed in toInterfaceOrientation since the UIApplication's statusBarOrientation will still point to the old orientation as it has not yet changed (since you're inside a will event handler).
Summary
There are several related posts to this, but each of them seem to indicate that you have to:
Look at [[UIScreen mainScreen] bounds] to get the dimensions,
Check what orientation you are in, and
Account for the status bar height (if shown)
Links
Iphone: Get current view dimensions or screen dimensions
IPhone/IPad: How to get screen width programmatically?
Objective C - how to get current screen resolution?
“Incorrect” frame / window size after re-orientation in iPhone or iPad
iPhone Dev SDK - get screen width
Working Code
I usually don't go this far, but you piqued my interest. The following code should do the trick. I wrote a Category on UIApplication. I added class methods for getting the currentSize or the size in a given orientation, which is what you would call in UIViewController's willRotateToInterfaceOrientation:duration:.
#interface UIApplication (AppDimensions)
+(CGSize) currentSize;
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation;
#end
#implementation UIApplication (AppDimensions)
+(CGSize) currentSize
{
return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation
{
CGSize size = [UIScreen mainScreen].bounds.size;
UIApplication *application = [UIApplication sharedApplication];
if (UIInterfaceOrientationIsLandscape(orientation))
{
size = CGSizeMake(size.height, size.width);
}
if (application.statusBarHidden == NO)
{
size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
}
return size;
}
#end
To use the code simple call [UIApplication currentSize]. Also, I ran the above code, so I know it works and reports back the correct responses in all orientations. Note that I factor in the status bar. Interestingly I had to subtract the MIN of the status bar's height and width.
Other thoughts
You could go about getting the dimensions by looking at the UIWindow's rootViewController property. I've looked at this in the past and it similarly reports the same dimensions in both portrait and landscape except it reports having a rotate transform:
(gdb) po [[[[UIApplication sharedApplication] keyWindow]
rootViewController] view]
<UILayoutContainerView: 0xf7296f0; frame =
(0 0; 320 480); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H;
layer = <CALayer: 0xf729b80>>
(gdb) po [[[[UIApplication
sharedApplication] keyWindow] rootViewController] view]
<UILayoutContainerView: 0xf7296f0; frame = (0 0; 320 480); autoresize
= W+H; layer = <CALayer: 0xf729b80>>
Not sure how your app works, but if you aren't using a navigation controller of some kind, you could have a UIView under your main view with the max height / width of parent and grows / shrinks with parent. Then you could do: [[[[[[[UIApplication sharedApplication] keyWindow] rootViewController] view] subviews] objectAtIndex:0] frame]. That looks pretty intense on one line, but you get the idea.
However, it would still be better to do the above 3 steps under the summary. Start messing with UIWindows and you'll find out weird stuff, like showing a UIAlertView will change UIApplication's keywindow to point at a new UIWindow that the UIAlertView created. Who knew? I did after finding a bug relying on keyWindow and discovering that it changed like that!
This is my solution code !This method can add to NSObject class's Categroy , or you can define a Top custom UIViewController class , and let all of your other UIViewControllers to inherit it .
-(CGRect)currentScreenBoundsDependOnOrientation
{
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
}
return screenBounds ;
}
Note, after IOS8 , as Apple Document of UIScreen's bounds property says :
Discussion
This rectangle is specified in the current coordinate space, which takes into account any interface rotations in effect for the device. Therefore, the value of this property may change when the device rotates between portrait and landscape orientations.
so for the consideration of compatibility , we should detect the IOS version and make the change as below:
#define IsIOS8 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
-(CGRect)currentScreenBoundsDependOnOrientation
{
CGRect screenBounds = [UIScreen mainScreen].bounds ;
if(IsIOS8){
return screenBounds ;
}
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
}
return screenBounds ;
}
Here's a handy macro:
#define SCREEN_WIDTH (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
In iOS 8+ you should use the viewWillTransitionToSize:withTransitionCoordinator method:
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
// You can store size in an instance variable for later
currentSize = size;
// This is basically an animation block
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Get the new orientation if you want
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
// Adjust your views
[self.myView setFrame:CGRectMake(0, 0, size.width, size.height)];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
// Anything else you need to do at the end
}];
}
This replaces the deprecated animation method that gave no information about size:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
As of iOS 8 screen bounds are now returned correct for current orientation. This means an iPad in landscape orientation [UIScreen mainScreen].bounds would return 768 on iOS <=7 and 1024 on iOS 8.
The following returns the correct height and width on all versions released.
-(CGRect)currentScreenBoundsDependOnOrientation
{
NSString *reqSysVer = #"8.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
return [UIScreen mainScreen].bounds;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat width = CGRectGetWidth(screenBounds) ;
CGFloat height = CGRectGetHeight(screenBounds) ;
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
screenBounds.size = CGSizeMake(width, height);
NSLog(#"Portrait Height: %f", screenBounds.size.height);
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
screenBounds.size = CGSizeMake(height, width);
NSLog(#"Landscape Height: %f", screenBounds.size.height);
}
return screenBounds ;
}
if you want the orientation dependent size and you have a view, you can just use:
view.bounds.size
I wrote category for UIScreen, that works on all iOS versions, so you can use it like this:
[[UIScreen mainScreen] currentScreenSize].
#implementation UIScreen (ScreenSize)
- (CGSize)currentScreenSize {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBounds.size;
if ( NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1 ) {
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if ( UIInterfaceOrientationIsLandscape(interfaceOrientation) ) {
screenSize = CGSizeMake(screenSize.height, screenSize.width);
}
}
return screenSize;
}
#end
Here is a Swift way to get orientation dependent screen sizes:
var screenWidth: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.width
} else {
return UIScreen.mainScreen().bounds.size.height
}
}
var screenHeight: CGFloat {
if UIInterfaceOrientationIsPortrait(screenOrientation) {
return UIScreen.mainScreen().bounds.size.height
} else {
return UIScreen.mainScreen().bounds.size.width
}
}
var screenOrientation: UIInterfaceOrientation {
return UIApplication.sharedApplication().statusBarOrientation
}
These are included as a standard function in a project of mine:
https://github.com/goktugyil/EZSwiftExtensions
float msWidth = [[UIScreen mainScreen] bounds].size.width*(IS_RETINA?2.0f:1.0f);
float msHeight = [[UIScreen mainScreen] bounds].size.height*(IS_RETINA?2.0f:1.0f);
if ( UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ) {
os->setWidth(MIN(msWidth, msHeight));
os->setHeight(MAX(msWidth, msHeight));
} else {
os->setWidth(MAX(msWidth, msHeight));
os->setHeight(MIN(msWidth, msHeight));
}
NSLog(#"screen_w %f", os->getWidth());
NSLog(#"screen_h %f", os->getHeight());
However, on iOS 8.0.2:
+ (NSUInteger)currentWindowWidth
{
NSInteger width = 0;
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
CGSize size = [UIScreen mainScreen].bounds.size;
// if (UIInterfaceOrientationIsLandscape(orientation)) {
// width = size.height;
// } else {
width = size.width;
// }
return width;
}
use -> setNeedsDisplay() for the view you want to resize.
Some improvements on the answers offered here, in Swift:
let interfaceOrientation = UIApplication.shared.statusBarOrientation // (< iOS 13)
let screenSize = UIScreen.main.bounds.size
let screenWidth = interfaceOrientation.isPortrait ? screenSize.width : screenSize.height
let screenHeight = interfaceOrientation.isPortrait ? screenSize.height : screenSize.width

Resources