Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9051533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:55:39+00:00 2026-06-16T12:55:39+00:00

Note : Things have moved on since this question was asked; see here for

  • 0

Note: Things have moved on since this question was asked; see here for a good recent overview.


Before auto layout, you could change the anchor point of a view’s layer without moving the view by storing the frame, setting the anchor point, and restoring the frame.

In an auto layout world, we don’t set frames any more, but constraints don’t seem up to the task of adjusting the position of a view back to where we want it to. You can hack the constraints to reposition your view, but on rotation or other resizing events, these become invalid again.

The following bright idea doesn’t work as it creates an “Invalid pairing of layout attributes (left and width)”:

layerView.layer.anchorPoint = CGPointMake(1.0, 0.5);
// Some other size-related constraints here which all work fine...
[self.view addConstraint:
    [NSLayoutConstraint constraintWithItem:layerView
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual 
                                    toItem:layerView 
                                 attribute:NSLayoutAttributeWidth 
                                multiplier:0.5 
                                  constant:20.0]];

My intention here was to set the left edge of layerView, the view with the adjusted anchor point, to half of its width plus 20 (the distance I want inset from the left edge of the superview).

Is it possible to change the anchor point, without changing the location of a view, in a view that is laid out with auto layout? Do I need to use hardcoded values and edit the constraint on every rotation? I do hope not.

I need to change the anchor point so that when I apply a transform to the view, I get the correct visual effect.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:55:40+00:00Added an answer on June 16, 2026 at 12:55 pm

    [EDIT: Warning: The entire ensuing discussion will be possibly outmoded or at least heavily mitigated by iOS 8, which may no longer make the mistake of triggering layout at the time that a view transform is applied.]

    Autolayout vs. View Transforms

    Autolayout does not play at all well with view transforms. The reason, as far as I can discern, is that you’re not supposed to mess with the frame of a view that has a transform (other than the default identity transform) – but that is exactly what autolayout does. The way autolayout works is that in layoutSubviews the runtime comes dashing through all the constraints and setting the frames of all the views accordingly.

    In other words, the constraints are not magic; they are just a to-do list. layoutSubviews is where the to-do list gets done. And it does it by setting frames.

    I can’t help regarding this as a bug. If I apply this transform to a view:

    v.transform = CGAffineTransformMakeScale(0.5,0.5);
    

    I expect to see the view appear with its center in the same place as before and at half the size. But depending on its constraints, that may not be what I see at all.

    [Actually, there’s a second surprise here: applying a transform to a view triggers layout immediately. This seems to me be another bug. Or perhaps it’s the heart of the first bug. What I would expect is to be able to get away with a transform at least until layout time, e.g. the device is rotated – just as I can get away with a frame animation until layout time. But in fact layout time is immediate, which seems just wrong.]

    Solution 1: No Constraints

    One current solution is, if I’m going to apply a semipermanent transform to a view (and not merely waggle it temporarily somehow), to remove all constraints affecting it. Unfortunately this typically causes the view to vanish from the screen, since autolayout still takes place, and now there are no constraints to tell us where to put the view. So in addition to removing the constraints, I set the view’s translatesAutoresizingMaskIntoConstraints to YES. The view now works in the old way, effectively unaffected by autolayout. (It is affected by autolayout, obviously, but the implicit autoresizing mask constraints cause its behavior to be just like it was before autolayout.)

    Solution 2: Use Only Appropriate Constraints

    If that seems a bit drastic, another solution is to set the constraints to work correctly with an intended transform. If a view is sized purely by its internal fixed width and height, and positioned purely by its center, for example, my scale transform will work as I expect. In this code, I remove the existing constraints on a subview (otherView) and replace them with those four constraints, giving it a fixed width and height and pinning it purely by its center. After that, my scale transform works:

    NSMutableArray* cons = [NSMutableArray array];
    for (NSLayoutConstraint* con in self.view.constraints)
        if (con.firstItem == self.otherView || con.secondItem == self.otherView)
            [cons addObject:con];
    
    [self.view removeConstraints:cons];
    [self.otherView removeConstraints:self.otherView.constraints];
    [self.view addConstraint:
     [NSLayoutConstraint constraintWithItem:self.otherView attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:self.otherView.center.x]];
    [self.view addConstraint:
     [NSLayoutConstraint constraintWithItem:self.otherView attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:self.otherView.center.y]];
    [self.otherView addConstraint:
     [NSLayoutConstraint constraintWithItem:self.otherView attribute:NSLayoutAttributeWidth relatedBy:0 toItem:nil attribute:0 multiplier:1 constant:self.otherView.bounds.size.width]];
    [self.otherView addConstraint:
     [NSLayoutConstraint constraintWithItem:self.otherView attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:0 multiplier:1 constant:self.otherView.bounds.size.height]];
    

    The upshot is that if you have no constraints that affect a view’s frame, autolayout won’t touch the view’s frame – which is just what you’re after when a transform is involved.

    Solution 3: Use a Subview

    The problem with both the above solutions is that we lose the benefits of constraints to position our view. So here’s a solution that solves that. Start with an invisible view whose job is solely to act as a host, and use constraints to position it. Inside that, put the real view as a subview. Use constraints to position the subview within the host view, but limit those constraints to constraints that won’t fight back when we apply a transform.

    Here’s an illustration:

    enter image description here

    The white view is host view; you are supposed to pretend that it is transparent and hence invisible. The red view is its subview, positioned by pinning its center to the host view’s center. Now we can scale and rotate the red view around its center without any problem, and indeed the illustration shows that we have done so:

    self.otherView.transform = CGAffineTransformScale(self.otherView.transform, 0.5, 0.5);
    self.otherView.transform = CGAffineTransformRotate(self.otherView.transform, M_PI/8.0);
    

    And meanwhile the constraints on the host view keep it in the right place as we rotate the device.

    Solution 4: Use Layer Transforms Instead

    Instead of view transforms, use layer transforms, which do not trigger layout and thus do not cause immediate conflict with constraints.

    For example, this simple “throb” view animation may well break under autolayout:

    [UIView animateWithDuration:0.3 delay:0
                        options:UIViewAnimationOptionAutoreverse
                     animations:^{
        v.transform = CGAffineTransformMakeScale(1.1, 1.1);
    } completion:^(BOOL finished) {
        v.transform = CGAffineTransformIdentity;
    }];
    

    Even though in the end there was no change in the view’s size, merely setting its transform causes layout to happen, and constraints can make the view jump around. (Does this feel like a bug or what?) But if we do the same thing with Core Animation (using a CABasicAnimation and applying the animation to the view’s layer), layout doesn’t happen, and it works fine:

    CABasicAnimation* ba = [CABasicAnimation animationWithKeyPath:@"transform"];
    ba.autoreverses = YES;
    ba.duration = 0.3;
    ba.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1)];
    [v.layer addAnimation:ba forKey:nil];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: This is a followup to my question here. I have a program that
NOTE: This is an old question and the answers here no longer works (since
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code
Note - I have moved the original post to the bottom because I think
I mean things like: FK1 -> 1FK2 -> 2PK Please, note that 1FK2 is
Note: Not sure if this is the right stack, please tell if I should
Note: I had another similar question about how to GZIP data using Ruby's zlib
NOTE: I have read Routing From the Inside Out AND the Engine Yard blog
This might be a weird question, but I'll try anyway. I set up my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.