iOSLayoutConstraint behaves strange

  1. last week
    Edited last week

    Hi everybody! I can't seem to get an iOSLayoutConstraint to work.

    This is what works fine:

    Dim ctrX As New iOSLayoutConstraint(imgTest, iOSLayoutConstraint.AttributeTypes.CenterX, iOSLayoutConstraint.RelationTypes.Equal, LineField, iOSLayoutConstraint.AttributeTypes.CenterX, 0.46, 0)
    
    ctrX.Active = True

    It sets the imgTest horizontal center to 46% of the LineField horizontal center. The problem is that 0.46 is not static but needs to be calculated. So I'm adding the calculation and replace the value 0.46 with intScale. The variable intLoc is being passed to this method from outside.

    Dim intScale As Double
    intScale = (100 - intLoc) / 10 * 0.18 + 0.1
    
    Dim ctrX As New iOSLayoutConstraint(imgTest, iOSLayoutConstraint.AttributeTypes.CenterX, iOSLayoutConstraint.RelationTypes.Equal, _
    LineField, iOSLayoutConstraint.AttributeTypes.CenterX, intScale, 0)
    
    ctrX.Active = True

    Now nothing happens. The imgTest does not move at all. I thought maybe it's because intScale has lots of decimals. So I'm doing this:

    Dim intScale As Double
    intScale = (100 - intLoc) / 10 * 0.18 + 0.1
    
    Dim strTemp As Text
    strTemp = intScale.ToText(Locale.Current, "0.00")
    intScale = Double.FromText(strTemp)
    
    Dim ctrX As New iOSLayoutConstraint(imgTest, iOSLayoutConstraint.AttributeTypes.CenterX, iOSLayoutConstraint.RelationTypes.Equal, _
    LineField, iOSLayoutConstraint.AttributeTypes.CenterX, intScale, 0)
    
    ctrX.Active = True

    Now I'm getting an exception when I try to create the iOSLayoutConstraint:

    NSLayoutConstraint for <UIImageView: 0x116d60880; frame = (305 45.5; 57 38); clipsToBounds = YES; opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x6000002497e0>>: A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant. Location attributes must be specified in pairs."

    I have no idea what I need to do. It should work in my opinion but it just does not.

  2. Dave S

    Aug 11 San Diego, California USA

    and people wonder why I hate Autolayout :)

  3. Michel B

    Aug 11 Pre-Release Testers, Xojo Pro RubberViews.com
    Edited last week

    Now that my Android app is reaching completion with another development tool, I am seriously considering the iOS tool from the same publisher, let alone not to have to deal with an unswitchable auto layout. Besides reusing most of the code, of course.

    A good deal of apps today don't waste time on rotating the display. They simply are portrait only.

    My app under Android is Landscape only.

    Xojo systematically forcing autolayout is typical of engineers "we know best" attitude.

  4. Dave S

    Aug 11 San Diego, California USA
    Edited last week

    Even Apple with Swift/Objc does not force the use of Autolayout, and my RAPID Swift UI builder takes full advantage of that

  5. Ulrich B

    Aug 11 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...
    Edited last week

    If I see it correctly you added these lines in the crashing example:

    @ChristophEmrich strTemp = intScale.ToText(Locale.Current, "0.00")
    intScale = Double.FromText(strTemp)

    You are writing intscale with the current locale (which, judged by your name, could be German, so it uses a comma as decimal separator) and reading it without locale, so it will look for a dot as separator. Did you examine intScale to check if it could be 0? That would explain the "multiplier of 0" message.

    Also, I do not see that you used AddConstraint to assign it to imgTest. Did you forget that?

    EDIT: And no, the fact that intScale is not rounded does not influence its functionality. You can safely remove that code again and retain precision.

  6. 7 days ago

    I did not use AddConstraint because it's not used in the Xojo Example project either. When I use AddConstraint for imgTest, the simulator shuts down and Xojo stops debugging without any kind of error message.

  7. It's really driving me crazy...

    Now I've tried something else. I created a layout constraint in the Auto-Layout (not in the code) and assigned a name "TestPosition". So now I just have to change the scale, right?

    intOffset = (100 - intLoc) / 10 * 0.18 + 0.1
    
    Dim c As iOSLayoutConstraint = Self.Constraint("TestPosition")
    c.Scale = intOffset

    Nope. Error message is that I cannot assign a value to the property Scale. This is nuts...

  8. Norman P

    Aug 12 Pre-Release Testers, Xojo Pro great-white-software.com/blog

    its hard to tell for sure with that tiny bit of code
    the error message would lead me to suspect that intScale is 0
    or linefield is nil

  9. Norman P

    Aug 12 Pre-Release Testers, Xojo Pro great-white-software.com/blog

    @Dave S and people wonder why I hate Autolayout :)

    fixed positions are easy to do in autolayout FWIW
    wouldnt even surprise me if under the hood fixed positioning is done that way using autolayout :)

  10. Ulrich B

    Aug 12 Pre-Release Testers, Xojo Pro Europe (Germany, Berlin) · xo...
    Edited 7 days ago

    @ChristophEmrich Error message is that I cannot assign a value to the property Scale. This is nuts...

    This is not nuts but clearly by design: iosLayoutConstraints are immutable; you cannot change their properties after creation. You have to replace the constraint with another one instead.

    This is, btw., not a weirdness of Xojo but Apple’s design. See https://developer.apple.com/documentation/uikit/nslayoutconstraint/1526920-multiplier?language=objc:

    @property(readonly) CGFloat multiplier;
  11. Dave S

    Aug 12 San Diego, California USA

    @Norman P fixed positions are easy to do in autolayout FWIW
    wouldnt even surprise me if under the hood fixed positioning is done that way using autolayout :)

    except that Left/Top are readonly meaning you cannot say

    ctrl.left=250

    not to mention even Xojo AL restricts some other values, such as height (makes sense of UISwitch, but not UITextField)

  12. Norman P

    Aug 12 Pre-Release Testers, Xojo Pro great-white-software.com/blog

    you can make an autolayout constraint that makes a control behaves as though it has absolute positioning

    set its left to the overall view containers left + a fixed offset

    and you can alter the constant offset whenever you want
    https://developer.apple.com/documentation/uikit/nslayoutconstraint/1526928-constant?language=objc

    this is why it would not surprise me if apple under the hood uses autolayout regardless

  13. 6 days ago

    Michel B

    Aug 13 Pre-Release Testers, Xojo Pro RubberViews.com

    My deepest regret is that there is no "manual" positioning, analogous to what XCode provides, where when I move a control in the IDE, Xojo does not try to guess relatives to.

    In a true fixed positions visual layout editor, controls are always relative to left and top, instead of changing to relative to other controls.

  14. 5 days ago

    @Ulrich B This is not nuts but clearly by design: iosLayoutConstraints are immutable; you cannot change their properties after creation. You have to replace the constraint with another one instead.

    Well, you can change the offset, not the scale though. But how do I replace a constraint? I am using the exact same code that's being used in the sample project "LayoutConstraintExample" and there it works every time. I mean, all I want to do is move an image a few times. That should certainly be possible.

  15. Jeremie L

    Aug 14 Pre-Release Testers, Xojo Pro, XDC Speakers Europe (France) - getpackr.io
    Edited 5 days ago

    To replace a constraint, you must set it as active=false and add a new constraint.

or Sign Up to reply!