1

I created an UIView and then added the Anchor constraints but I have a problem when I want to read the values ...

In this case as you see, I created an NSLayoutConstraint property to get the Anchor Width of my uiview ... I then created a CGFloat that contains constraint but my NSLog always returns me a ZERO value.

where am I wrong? how can I get the width values of my UIView assigned to the Anchors?

 UIView *trackLine = [[UIView alloc] init];
    trackLine.backgroundColor = [self trackLineColor];
    trackLine.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:trackLine];

    [trackLine.topAnchor constraintEqualToAnchor:mediaTitle.bottomAnchor constant:25].active = YES;
    [trackLine.rightAnchor constraintEqualToAnchor:self.rightAnchor].active = YES;
    [trackLine.heightAnchor constraintEqualToConstant:1].active = YES;
    self.width = [trackLine.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:.8];
    self.width.active = YES;

    CGFloat trackLineLenght =  self.width.constant;
   NSLog(@"TRACK LINE %f", trackLineLenght );

NSLog Result:

**2017-10-21 17:10:35.096562+0200  [5501:1994879] TRACK LINE 0.000000**
kAiN
  • 2,559
  • 1
  • 26
  • 54
  • What is the value of `self.widthAnchor` at that point? Is it possible that `self` does not have an intrinsic size, and its frame has not yet been set? – DonMag Oct 21 '17 at 15:40
  • @DonMag In a few words I was trying to follow this tutorial using uiview instead of CALayer but I still get the wrong values and I do not know why https://www.raywenderlich.com/36288/how-to-make-a-custom-control – kAiN Oct 21 '17 at 15:51
  • OK ... so ... as I asked in my first comment... *What is the value of `self.widthAnchor` at that point?* – DonMag Oct 21 '17 at 15:59
  • nslog on the cursor position is 20.5 but the self.currentValue is value is 30 – kAiN Oct 21 '17 at 16:02

1 Answers1

1

OK - a little confusion...

First, don't use "width" as a property name... very confusing, since width is used in so many places already.

So, let's assume you have:

    @property NSLayoutConstraint *trackLineWidthConstraint;

A .widthAnchor doesn't really have a width in terms of "what's the width of the anchor." The way you are defining your constraints:

    self.trackLineWidthConstraint = [trackLine.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:.8];

Says "set the .trackLineWidthConstraint property to 80% of the width of self. So, whenever the actual width of self changes, the actual width of your trackLine view will change to 80% of the new width.

The .constant is Zero. If it was something other than zero, that value would be added after calculating 80%. For example:

self.trackLineWidthConstraint = [trackLine.widthAnchor constraintEqualToAnchor:self.widthAnchor multiplier:.8];
// if self is 200-pts wide, trackLine will be 160-pts

self.trackLineWidthConstraint.constant = 10
// trackLine width is now (200 * 0.8) + 10, or 170-pts

If you want to get the current width of trackLine, you can get it from its .frame (after auto-layout has finished).

Hopefully, that didn't just make it more confusing :)

DonMag
  • 69,424
  • 5
  • 50
  • 86
  • ok but how can I get the frame after the automatic layout? why when I call view.frame.size.width never gives me a value ... am I wrong in the location where I call the frame? – kAiN Oct 21 '17 at 16:41
  • if we can go 2 minutes chat I'll see my code to understand why my slider does not answer ... I'm going out of my mind – kAiN Oct 21 '17 at 16:59
  • Join me in chat here (was a previous chat with someone else...) https://chat.stackoverflow.com/rooms/156596/discussion-between-donmag-and-as-diu – DonMag Oct 21 '17 at 17:03