Reputation: 14380
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?
Upvotes: 6
Views: 5874
Reputation: 1168
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)
}
}
Upvotes: 2
Reputation: 363
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.
Upvotes: 1
Reputation: 1929
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
}
Upvotes: 14
Reputation: 14380
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.
Upvotes: 11
Reputation: 723
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:
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.
Upvotes: 0