In a nut shell. I am trying to add constraints to resize a date picker and text view. Without the text view the date picker does what I want it to do (move to the top and centred), I add the text view it as soon as I add any for of constraint, when I run it I get this. What am I missing or getting wrong
Question posted in Xcode
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
Whether you're new to Xcode or an experienced developer, our archive has everything you need to know about this integrated development environment (IDE). From basic functionalities to advanced features, our archive covers a wide range of Xcode-related questions and answers. Browse our archive now and find solutions to your Xcode questions, and take your app development skills to the next level
2
Answers
You didn’t specify compatibility version but let’s assume that it is iOS 14 since you want to use compact date picker.
You can change Preferred Style to
.compact
in storyboard to preview how it will look like.But, ups… 😦 now storyboard warns you that there is problem with your layout. More specifically, date picker doesn’t know how heigh it should be.
There are several ways how to fix problem with ambiguous height of view. But for some reason, auto-layout doesn’t work for this view until you manually set its height with height constraint. 🤯
"Auto-layout" solution
But, there is also one "hacky" solution which I’ve just created. But to make it work we need to embed out views inside vertical
UIStackView
(ideally with center alignment)and our date picker needs to have required vertical content hugging priority
Now start with creating custom subclass of
UIDatePicker
and specify itsintrinsicContentSize
to height of first subview of first subview (private hierarchy, but 🤷🏻♂️)…So create this subclass and don’t forget to set this subclass in storyboard.
Note that I also set its width to this subview’s width so it also works when you have for example date picker in vertical stack view with center alignment.
Now one more thing you have to do is to invalidate its intrinsic content size in view controller’s
viewDidLayoutSubviews
I feel like that this new compact date picker is something which Apple should definitely change, but 🍎…
Apple provided two new date picker appearances (inline and compact), changed the default appearance to one of these new appearances (compact) and did not provide a way to select the old appearance (wheels) until a later iOS version (at 13.4 through
UIDatePicker.preferredDatePickerStyle
). But what is really causing your problem here is that Apple seemingly forgot to implementintrinsicContentSize
for these new appearances which is a problem if your constraints fail at positively determining a height. Robert Dresler’s answer is on the right track (trying to bring back a usefulintrinsicContentSize
implementation) but it relies on knowledge of the internal structure of these views. As suchUntil Apple finally fixes theses controls I had great success with the following
Same idea as Robert (provide a better
intrinsicContentSize
implementation) only mine does not rely on any specific knowledge of the view’s internal structure and usesUIView.systemLayoutSizeFitting
to calculate a size.