6

I have a UITextField where I set the inputView to a custom view. This has a standard height of 216. I would like to set this to something like 300, or half the screen.

Using constraints is not an option, as it conflicts with the default 216 and it discards my constraint.

Setting the frame also does not work.

Is there some way to set this to a higher value?

vrwim
  • 13,020
  • 13
  • 63
  • 118

5 Answers5

14

You need subclassed UIInputView and overrided allowsSelfSizing for return value true, like this

class MyInputView: UIInputView {
// ... other code
    override var inputViewStyle: UIInputViewStyle { get { return .default } }
    override var allowsSelfSizing: Bool { get { return true } set{} }
// ... other code
}
EvGeniy Ilyin
  • 1,817
  • 1
  • 21
  • 38
  • 2
    This is the true answer to the question. Kudos. – applejack42 May 05 '18 at 12:09
  • thats the right way to do it, just make sure that your input view has defined height with constraints. (in my case i introduced height constraints for the child views and vertical distances between them and view bounds) – Stoyan Jun 14 '18 at 09:33
11

I got it to work by setting the autoResizingMask to None, and setting the frame to the desired frame (using UIScreen.mainScreen to get the width of the screen). I did both these things before layoutSubviews gets called.

vrwim
  • 13,020
  • 13
  • 63
  • 118
1

Setting the frame of the customInputView like so, worked for me.

datePicker.frame = CGRect(x: view.frame.minX, y: view.frame.maxY - 250, width: view.frame.width, height: 250)

Here I have used datePicker as an inputView for my textField.

Faizyy
  • 353
  • 2
  • 15
1

You can do it using autolayout and without subclassing UIInputView. Just a regular UIView does the trick. In your subclass you have to add the following:

class CustomView: UIView {

override var intrinsicContentSize: CGSize {
    CGSize(width: UIView.noIntrinsicMetric, height: 300)
}

}
Julian Osorio
  • 1,116
  • 1
  • 12
  • 30
0

Since you mention that you want to use AutoLayout in your Reddit thread about this, there are a few options. Just to clarify, you have a UITextField and you're setting it's inputView to a custom view programmatically? Are you trying to change the height of the view programmatically based on the screen/window height?

If you want to use AutoLayout then constraints are pretty much your only option. Just trying to update the Frame of the view won't do anything because it will favor the preset constraint, as you discovered. What you need to do is change the constant value of the UITextField's height constraint.

First you need to add a height constraint to the UITextField in Interface Builder. You can do that by clicking the constraints button in the lower-right of the IB window: Add Constraint UI

Then just check the Height box, enter whatever value you want and click Add Constraints.

Now you need to get a reference to that constraint into your ViewController. First, open the header for the VC in question in the Assistant Editor. Then, find the constraint in question in the Scene Navigator on the left side of the screen, right-click it, and drag it into your header just as you would any other IBOutlet.

Now, in your code you will have a reference to the height constraint for that view and you can update it using constrant.constant = 100; or whatever value you want to use. Don't forget to call [[self view] layoutSubviews] afterwards to let the OS know that it needs to update the layout.

I know this is pretty much the only way I have found to reliably update constraints and layout programmatically. Hopefully it will work for you, too.

Stoph
  • 693
  • 1
  • 5
  • 20
  • I think you misunderstood, I want to change the height of my `inputView` itself (the keyboard). – vrwim Oct 22 '15 at 19:12
  • 1
    Ahh, yes, my bad. Have you tried updating the `autoresizingMask` value for your custom `inputView`? See [here](http://stackoverflow.com/questions/6042060/custom-keyboard-inputview-how-to-change-the-keyboard-size) and [here](https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html) for reference. – Stoph Oct 22 '15 at 19:22