Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to change UIInputView height

I have a simple UIInputViewController subclass with only two overridden methods. I use this input view controller as inputAccessoryViewController on my UIViewController subclass which becomes first responder. I try to specify height of inputView by adding constraint as Apple documentation recommends. Problem is that my constraint doesn't work and I get autolayout exception when my constraint is being added

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
...
(
    "<NSLayoutConstraint:0x178aa1d0 V:[UIInputView:0x178a4ae0(0)]>",
    "<NSLayoutConstraint:0x178b9520 V:[UIInputView:0x178a4ae0(500)]>"
)
Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x178b9520 V:[UIInputView:0x178a4ae0(500)]>

Which I think means that system already added a zero height constraint to the input view (because it is created with zero height). Now they conflict and autolayout breaks my constraint to fix the issue.

When I try to use it as inputViewController of my view controller (just for test purposes), I get same exception but instead of zero height it is 216 px. It also breaks my constraint and the height remains default.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.inputView.translatesAutoresizingMaskIntoConstraints = NO;
    self.inputView.backgroundColor = [UIColor redColor];
}

- (void)updateViewConstraints {

    CGFloat _expandedHeight = 500;
    NSLayoutConstraint *_heightConstraint = 
    [NSLayoutConstraint constraintWithItem:self.view
                                 attribute:NSLayoutAttributeHeight
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:nil
                                 attribute:NSLayoutAttributeNotAnAttribute
                                 multiplier:0.0
                                   constant: _expandedHeight];
    [self.inputView addConstraint: _heightConstraint];

    [super updateViewConstraints];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.view setNeedsUpdateConstraints];
}

As a result, I am not able to change input accessory view height. Has anyone succeed in it? Obviously, Apple documentation provides no help...

like image 334
hybridcattt Avatar asked Sep 30 '14 11:09

hybridcattt


3 Answers

Since iOS 9.0 this can be solved with inputView.allowsSelfSizing = YES;

like image 107
Rob Zombie Avatar answered Nov 03 '22 17:11

Rob Zombie


I don't know if this is the issue, but the problem may come from the 0.0 multiplier you are setting on _heightConstraint. Try to change it to 1.0.

It would look like this:

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

Hope this helps!

like image 3
cris Avatar answered Nov 03 '22 18:11

cris


When you make a view the input accessory view of a UITextView, as soon as the text view becomes first responder, a height constraint is added automatically set to whatever was sent as the frame height. For example:

let inputView = UIInputView(frame: CGRectMake(0, 0, view.bounds.width, 200), 
    inputViewStyle: .Default)
someTextView.inputAccessoryView = inputView
someTextView.becomeFirstResponder()
assert((inputView.constraints().last as NSLayoutConstraint).constant == 200)

You can modify this constraint after the fact.

like image 1
moger777 Avatar answered Nov 03 '22 19:11

moger777