Default width and height of a View with wrap_content - android-view

When I create a custom View, and I draw it by adapting sizes in View.onSizeChanged(), what is the default size of the View? I my layout I have set the android:layout_width and android:layout_height to wrap_content.

The default size is 0. Unless it has subviews (and is derived from a layout class which defines onLayout), in which case the default size is the sum of its children. If you want to use wrap_content on a custom view that isn't just a collection of child views, you need to override on onLayout and/or onMeasure.

Related

Create custom view with constraints and initializing with frame

I need to create a custom view like this (small view over current controller background) and load it programatically:
I used to create full-size custom views, and the problem I have now is that I'm not sure about how to handle frame vs constraints in smaller ones that depend also on the parent view controller.
I would need to set the frame of the view in its init(), so it has some margins in relation to the background frame, and it also needs to have a flexible height depending on the size of the text (so I can't use only a fixed frame, its size must depend on the text)
If I pass the frame in its init(), it doesn't respect the flexible height, and if I use translatesAutoresizingMaskIntoConstraints = false, it doesn't respect the position over the background view.
Is there any way to combine both?

How to create a View that height depends on child views?

This is the same as asking how to achieve wrap_content in iOS.
However, I found no suitable solution. Almost all solutions suggest to override intrinsicContentSize(). What if the view does not know any information about its child's height?
The case is like this:
I want to create a BaseDialogViewController that is responsible for blurring the background and displaying a dialog container. Then if I want to implement a dialog, I will extends BaseDialogViewController and add the dialog content to the container view.
So, the container needs to adjust its height according to the total height of the content inside of it.
How do I achieve this?
Just set constraints for all borders from the child view to the container view. You should ensure that the child view can compute its size from its contained elements, by setting constrains for all borders to its child elements, and so on.
The container view should only have additional constraints for its position (e. g. centering in its parent view).
The rest is done by Autolayout. It will calculate the size from the innermost elements with fixed or intrinsic sizes (e.g. labels, text fields, buttons) to the outer elements.
I realized that if child views height is specified, or has an intrinsic height,
you can set constraint from parent's bottom to child's bottom, and by setting this, the parent's height will be able to vary according to the bottom position of child's view.
I found this out from this answer.

How do I auto increase the parent view size

I have a parent view which has so many child views most of the child views size are fixed except three labels.
so these three labels height can differ according to its content.
I want to change the parent view height when these label height changes and I want to change the position of other views whose that follows the label.
I think when I assign the label some contetn I should read its frame and get the height and I should change the postiion of following element according to that and I should change the parent view height according to that.
I want to do this the right way
Please let me knwo the right way to do this
Update:
what I'm trying to achieve is kinda like below image and it has so many fields like this

Intrinsic Content Size vs Alignment Rect

When would you need to override alignmentRect instead of intrinsicContentSize in some UIView subclass?
Is it just for when the position as well as the size is different? If you supply an alignmentRect, is the content size ignored?
intrinsicContentSize is used to tell the layout system what the size of a view is. Use this to inform the layout system how large to draw a view based on its contents. Content size, not position.
For positioning, there are two things you should look at:
layoutMargins, which is used to determine the layout of the inside of a view (i.e. padding on the left and right of a stackView's contents), and alignmentRectInsets, which is used to inform the object holding your view how it should align your view. For example, if you have a shadow or attached view (like a notification dot), you might want to lay your view out centered on the primary feature, not including the shadow or dot's width/height.
The article Auto Layout in iOS 8 - Layout Margins at Carpeaqua does a good job of explaining layout margins with examples, and the article Auto Layout and Alignment Rectangles at Use Your Loaf does a good job of explaining and showing why you might want to use an alignmentRectInsets.

UIViews with ambiguous height

I have a collection view inside of a UIView. I need to have the collection view communicate to the UIView the height it should be depending on the amount of content inside the collection view.
I need to then let this UiView (white box) know how tall it should be. I believe I did this part correctly in my setup function, but I'm not certain (I'm new to coding).
I need to have the collection view communicate to the UIView the height it should be depending on the amount of content inside the collection view
That height is the layout's collectionViewContentSize height. But note that this height can grow beyond the size of the window, since this is a scroll view - the whole point is that the size of the content can be greater than the size of the collection view. Thus it might be better to ask yourself why you think you need to do this.

Resources