4

I want to make a custom keyboard in iOS 8. I want to change the size of it when the phone rotates to landscape mode.

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
duration:(NSTimeInterval)duration
{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    if ((toInterfaceOrientation==UIInterfaceOrientationPortrait) || (toInterfaceOrientation==UIInterfaceOrientationPortrait))
    {
        [def setObject:@"No" forKey:@"LandScape"];
        [def synchronize];
        islandscapemode = NO;
        NSLayoutConstraint *_heightConstraint =
        [NSLayoutConstraint constraintWithItem: self.view
                                     attribute: NSLayoutAttributeHeight
                                     relatedBy: NSLayoutRelationEqual
                                        toItem: nil
                                     attribute: NSLayoutAttributeNotAnAttribute
                                    multiplier: 0.0
                                      constant: 266];

        [self.view addConstraint:_heightConstraint];
           }
    else
    {
        [def setObject:@"Yes" forKey:@"LandScape"];
        [def synchronize];

        NSLayoutConstraint *_heightConstraint =
        [NSLayoutConstraint constraintWithItem: self.view
                                     attribute: NSLayoutAttributeHeight
                                     relatedBy: NSLayoutRelationEqual
                                        toItem: nil
                                     attribute: NSLayoutAttributeNotAnAttribute
                                    multiplier: 0.0
                                      constant: 266];

        [self.view addConstraint:_heightConstraint];

        self.view.backgroundColor=[UIColor greenColor];

        islandscapemode = YES;

    }
    //  [self determineKeyboardNib:toInterfaceOrientation];
}

But it doesn't work. What should I do?

Andrew
  • 15,357
  • 6
  • 66
  • 101
Ikambad
  • 104
  • 5
  • what should i have to do reduce the size of view when it became in landscape mode? – Ikambad Sep 01 '14 at 12:12
  • 2
    What does "it doesn't work" mean? The keyboard stayed the same size? You got error messages? The device exploded in your hand and you typed this question with your one good hand? – skrrgwasme Sep 18 '14 at 23:37
  • You should in -[viewDidLoad], add the constraint to the view and just change the constraint constant value ,not by add a new constraint that will cause constraint wrong. And in your constraint code ,you should set the super view to close the autosizing – Feng Lin Jan 28 '15 at 01:51

1 Answers1

1
willRotateToInterfaceOrientation:duration:

Was deprecated in iOS 8. Instead, try and use

viewWillTransitionToSize:withTransitionCoordinator:

iOS Developer Library: viewWillTransitionToSize

This function gives you a reference to the new size of the container's view and a transition coordinator. You can then resize your keyboard to the new screen size.

There is also a function attached to the transition coordinator that allows you animate views while the screen is rotating and/or a completion block to do stuff after the screen rotates.

iOS Developer Library: UIViewControllerTransitionCoordinator

animateAlongsideTransition:completion:

and

animateAlongsideTransitionInView:animation:completion:

As for your original question, did you try calling layoutIfNeeded on the view after you set up the constraints?

RPK
  • 1,830
  • 14
  • 30