How to create a custom UIView that defines a baseline?

Using the NSLayoutConstraint class, you can create a baseline-based constraint ( NSLayoutAttributeBaseline ). However, I have not seen any documentation that describes how a UIView actually provides a baseline value to an automatic layout system.

If I wanted to create a custom subclass of UIView that defines a baseline, how do I do this? NSView defines the baselineOffsetFromBottom method, which I assume is somehow related to OS X, but how does it work on iOS?

+6
source share
2 answers

From the UIView docs:

viewForBaselineLayout

Gets the view used to satisfy basic constraints.

- (UIView *)viewForBaselineLayout

Return value

The view that the constraint system must use to satisfy basic constraints

+6
source

I just used the Hopper disassembler to look at UILabel in iOS 8.1, and it implements the _baselineOffsetFromBottom and _firstBaselineOffsetFromTop , which in turn are called -[UIView nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:] , so the UIKit methods often have, UIKit X, they simply are not exposed to the public.

_baselineOffsetFromBottom and _firstBaselineOffsetFromTop are implemented by UIView (return 0), UILabel and UITextView. There is also a class called _UIGlintyStringView that implements _baselineOffsetFromBottom ; no other UIKit classes have these methods.

Btw, when the baseline of the view changes, it does something like:

 __UITagLayoutConstraintsForConstantChangeForSelectedAttributes(self, __UILayoutAttributeIsBaselineAttribute) 

It does not seem that there is anything special that could not be published; perhaps they did not feel there or did not want to prevent people from writing their own labels. UILabel is a fairly sophisticated class that includes a custom CoreAnimation level (_UILabelLayer) and crap, including quite a bit of code to support accessibility.

+6
source

Source: https://habr.com/ru/post/944395/


All Articles