IOS Customkeyboard getting wrong size in Landscape mode

前提是你 提交于 2019-12-06 09:33:08

You would be better to not keep removing and adding the height constraint. Instead add it the first time and then after that simply change its value and set the view as needing layout and then do the layout.

Also think your might be working out the orientation wrong by using bounds before the transition occurs.

Updated your methods as follows:

- (void)viewWillTransitionToSize:(CGSize)size 
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{
  if (size.width < size.height){
    //Keyboard is in Portrait
    isPortrait=YES;
    [self LoadKeyboardview];
    [self updateCustomHeight];
  }
  else{
    // New orientation is landscape
    isPortrait=NO;
    [self LoadKeyboardview];
    [self updateCustomHeight];
 }
}


-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                               duration:(NSTimeInterval)duration{
  if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
      toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
    //Keyboard will be Portrait
    isPortrait=YES;
    [self LoadKeyboardview];
    [self updateCustomHeight];
  }
  else{
    //Keyboard will be Landscape
    isPortrait=NO;
     [self LoadKeyboardview];
    [self updateCustomHeight];
  }
}

-(void)updateCustomHeight
{
  if(isPortrait){
    expandedHeight = 258;
  }
  else{
    expandedHeight = 156;
  }

  // If no constraint add one, or if one exists update it only
  // if the height needs to change.
  if (self.heightConstraint == nil) {
    self.heightConstraint = 
      [NSLayoutConstraint constraintWithItem:self.view      
                                   attribute:NSLayoutAttributeHeight 
                                   relatedBy:NSLayoutRelationEqual toItem:nil 
                                   attribute:NSLayoutAttributeNotAnAttribute 
                                  multiplier:0.0 
                                    constant:expandedHeight];
    [self.view addConstraint:self.heightConstraint];
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
  }
  else if (self.heightConstraint.constant != expandedHeight){
    self.heightConstraint.constant = expandedHeight;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
  }
}

If you use "auto layout" in designing of keyboard in xib then there is no need to add height constraint,it automatically adjust according to default keyboard size.I simply load the view and not add any height constraint and i works fine for me in all devices.

 override func viewDidLoad() {
        super.viewDidLoad()
        let nib = UINib(nibName: "VSPKeyboardViewController", bundle: nil)
        let objects = nib.instantiateWithOwner(self, options: nil)
        view = objects[0] as! UIView

}

override func updateViewConstraints() {
        super.updateViewConstraints()
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!