Question

In one of my ViewControllers, i am rotating a UIImageView using this method:

    UIImageView *tmp = (UIImageView *)[self.view viewWithTag:1];
    if(tmp)
        [tmp removeFromSuperview];

    // Create new label and add it back (with proper rotation)
    self.arrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 157, 320, 320)];   
    self.arrowImage.image = [UIImage imageNamed:@"image"];
    self.arrowImage.tag = 1; // Add back with same tag
    [self.view addSubview:self.arrowImage];
    self.arrowImage.transform = CGAffineTransformMakeRotation( ... );

Apparently, one needs to remove and add back the subview (i.e. UIImageView) to make it rotate around its center. At least that's the only way I could get it to work. Other/better ways of doing it would be greatly appreciate.

However, my question is: how do I add the UIImageView as a subview so it's behind, say, a UILabel that I placed above it in my storyboard?

Was it helpful?

Solution

You can use:

– insertSubview:aboveSubview:
– insertSubview:belowSubview:
– insertSubview:atIndex:

Instead of addSubView, or use sendSubviewToBack:

OTHER TIPS

I think you are looking for

[topViewAndBottomViewParentView insertSubview:topView aboveSubview:bottomView]

[topViewAndBottomViewParentView insertSubview:bottomView belowSubview:topView]

Adding the subview to the subview-hierarchy at a given point using insertSubview:aboveSubview: or insertSubview:belowSubview: or insertSubview:atIndex: is certainly a good startnig point.

However, when you then rotate it, its subsequent subviews would probalby rotate as well. (depending on how you rotate it).

I suggest to think of a different approach. 1. add all subviews (as a stack of subview) to self.view (or whatever common superview you have) that are supposed to be displayed below the one view that you want to rotatte. 2. add the view that you want to rotate to the common superview (self.view) 3. add the view (or the view stack) to the common superview too.

By doing so the three view stacks (meaning view plus its subview plus its subview ...) are siblings as subviews to self.view. But they overlap. The sequence of adding them will care for that those added first are visible under the rotating view and the views added as third are visible on top but the one in the middle should be able to be rotated without impacting the other views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top