How to dynamically change a UIButton as a subitem of a UIImageView while rotating

I am working on an iPad application that allows you to control different things in a prototype smart home. For example, it allows you to turn on and off the light. For this, I did UIImageView, which shows the floor plan of the house and added UIButtonsas pods for each lamp that can be switched.

enter image description here

As you can see, the buttons are located perfectly on the floor plan, using the method setFramefor each UIButton. However, when I turn the iPad to portrait orientation, the following happens:

enter image description here

Buttons, obviously, still have the same origin, however this does not apply to changing the position of the image.

The floor plan image has the following settings for struts and springs:

enter image description here

and its content mode is set to Aspect Fit.

My question

as I dynamically rearrange each UIButton, so that it has the same relative position. I suppose I need to handle this in the delegate method {did/should}AutorotateToInterfaceOrientation.

It should be noted that it UIImageViewscales and to handle this, I implemented the delegation method scrollViewDidZoomas follows:

for (UIView *button in _floorPlanImage.subviews) {       
    CGRect oldFrame = button.frame;
    [button.layer setAnchorPoint:CGPointMake(0.5, 1)];
    button.frame = oldFrame;
    button.transform = CGAffineTransformMakeScale(1.0/scrollView.zoomScale, 1.0/scrollView.zoomScale);
}

Thank you in advance!

+5
source share
1 answer

I find the best way to create layouts in a method - (void) layoutSubviews. You will have to subclass your UIImageView and override the method.

This method will be called automatically when your frame is changed, and it will also be called the first time your view is presented.

, , , .

+1

All Articles