How to hide UIView on the right side of the program

I am desperately trying to insert one of my UILabels on the right edge of this supervisor, while the width of the label is variable (this time, so the thing gets bigger and needs to expand to the left, this is done using sizeToFit inside the label when the text is set).

So far I have tried many things, but closest I got:

_elapsedTimeRightConstraint = [NSLayoutConstraint constraintWithItem:_elapsedTimeView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-150]; 

While the label is initially set to a width of 150 pixels. But when I change the constant, everything goes to hell.

 _elapsedTimeRightConstraint.constant = (_elapsedTimeView.frame.size.width * -1); [self layoutIfNeeded]; 

So my question is how to align the trailing edges of the view and its supervisor (so it sticks to the right) when the width of the view is constantly changing. I used FLKAutoLayout in the project, so if it can be done with this wireframe structure, itโ€™s just great, but the basic autostart solution will also be awesome!

+5
source share
3 answers

First, make sure that translatesAutoresizingMaskIntoConstraints set to NO if you are creating the label programmatically.

The first limitation you need is "label.trailing = superview.trailing".

 [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTrailing multiplier:1.f constant:0.f] 

This will snap the right edge (left to right) to the label on the right edge of the supervisor.

Now you need a constraint for position Y

In my test, I vertically centered the label with the following restriction:

 [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.f constant:0.f] 

Now comes the trick!

Each time you change the text on the shortcut, you need to recalculate frames using AutoLayout.

 [superview setNeedsLayout]; [superview layoutIfNeeded]; 

Autostart will be:

1) Set a new size label (based on its text).

2) Adjust the size of the mark.

3) Connect the trailing edge of the label to the trailing edge of the supervisor.


Further research

The problem with UILabel is that when you use AutoLayout and you set the text, its intrinsicContentSize changes, but does not start the layout update.

The way to enforce this without subclassing UILabel is to use Objective-C runtime.

 @interface UILabel (AutoLayout) - (void)swz_setText:(NSString*)text; @end @implementation UILabel (AutoLayout) + (void)load { NSLog(@"Swizzling [UILabel setFont:]..."); Method oldMethod = class_getInstanceMethod(self, @selector(setText:)); Method newMethod = class_getInstanceMethod(self, @selector(swz_setText:)); method_exchangeImplementations(oldMethod, newMethod); } - (void)swz_setText:(NSString*)text { if (![text isEqualToString:self.text]) { [self setNeedsLayout]; } [self swz_setText:text]; //This now points to "setText:" - not a mistake! } @end 

In this category, I improve the implementation of setText: by calling setNeedsLayout if the text changes.

Now you just need to call layoutIfNeeded in the supervision to recount / rebuild the label frame.


Click here for the playground (Swift 2.0 - Xcode 7) where I checked my code.

Hope this helps.

+4
source

Hi, here are a few points to achieve the desired result:

  • Set the NSLayoutConstraintTrailing constant to 0 , and the leading constraint, NSLayoutAttributeLeading, sets it to greater than or equal . the value you want.
  • Use NSLayoutConstraintTrailing and NSLayoutAttributeLeading instead of right and left to handle other languages. Hope this helps.
0
source
 [yourLabel sizeToFit]; CGRect frame = yourLabel.frame; frame.x = parentView.frame.size.width - yourLabel.frame.size.width; yourLabel.frame = frame; 

It completely ignores the "best practice" of using autostart functions, but if you just can't take it anymore ... this should work .; -)

0
source

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


All Articles