How can I have UIButtons follow the resizing of a static image?

51 views Asked by At

I have a static image for my ThermostatSettingViewController and have assigned UIButton positions in interface builder to correspond with the visual layout on the png image. I have to set the Do not resize flag on IB because if the image is scaled to fit in the UIView of a larger screen the UIButtons are no longer in the right places. As the UIButtons are set as "No Name" and just an invisible button you can't find them.

I've tried normal XIB positioning, constraints, most everything I can think of, with no success.

Edit:

If no one has found a way, the only way I can think of is to the the static image and determine the center, by point count, of where each button and label should be dynamically placed. Then find out the current UIView size in points and calculate (just multiplication really) the new X,Y value for the center of each button.

The pain is, of course, is to painstakingly count out each pixel/point on the base image and determine a baseline allocation X,Y for each Label or Button.

If that is the case, I'm willing to give it a go but with two other questions:

  1. Does anyone have a utility that can output X,Y coordinates from my base image. Something like where the cursor is at. I can then store each in a dictionary and lookup the position of the corresponding Label/Button.

  2. is the .bounds parameter enough to tell me how big the X,Y view is for the image in points? So, does anyone have any code that can dynamically position a control (like constraints do) given an X,Y coordinate in the current view?

Any help would be appreciated.

Does anyone have a method that might be able to calculate the relative position of where the buttons should be placed based on the returned size of the ImageView size?

Here is an example of the code in Objective-C.

- (IBAction) CoolDownButtonPressed:(id)sender {
    int number = 0;
    number = [self convertStringtoInteger:_CoolSetting.text];
    number--;
    NSString *mystring = [self convertIntegertoString:number];
    if (![_HoldMode.text isEqualToString:@"Holding"]) {
        cmdString = [NSString stringWithFormat:@"temp set cool %@", mystring];
        _CoolSetting.textColor = [UIColor systemBlueColor];
    } else {
        _CoolSetting.textColor = [UIColor systemOrangeColor];
        [General wobbleField:_CoolSetting];
    }
    _CoolSetting.text = mystring;
}

- (IBAction) statModeOffButtonPressed:(id)sender {
    NSString *command = @"temp set fan auto";
    [self sendCommand:command];
}

- (IBAction) statModeAutoButtonPressed:(id)sender {
    NSString *command = @"temp set mode auto";
    [self sendCommand:command];
}

- (IBAction) statModeCoolButtonPressed:(id)sender {
    NSString *command = @"temp set mode cool";
    [self sendCommand:command];
}

- (IBAction) statModeHeatButtonPressed:(id)sender {
    NSString *command = @"temp set mode heat";
    [self sendCommand:command];
}

- (IBAction) fanModeAutoOnButtonPressed:(id)sender {
    NSString *command = @"temp set fan on";
    [self sendCommand:command];
}

- (IBAction) fanModeAutoAutoButtonPressed:(id)sender {
    NSString *command = @"temp set fan auto";
    [self sendCommand:command];
}

- (IBAction) holdModeOnButtonPressed:(id)sender {
    NSString *command = @"temp set hold on";
    [self sendCommand:command];
}

- (IBAction) holdModeOffButtonPressed:(id)sender {
    NSString *command = @"temp set hold off";
    [self sendCommand:command];
}

And here is the image that I'm displaying. The problem is that on 'say an iPad, the image displays in the upper left corner as I can't resize it.

Thermostat Setting Image

0

There are 0 answers