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 8279283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:17:00+00:00 2026-06-08T09:17:00+00:00

Im really having a problem with my image with button frame, What I want

  • 0

Im really having a problem with my image with button frame,
What I want is:
1. When it is selected it will have a checkmark
2. When the view is dismissed then returned back to the previous screen it will still have the check in the image selected
3. When I tap the selected image or another image it will remove the checkmark.

I was able to do it but I’m having problem with the setting of image:

What I want is this:

enter image description here

UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
[myButton setEnabled:NO];

But this is the function I needed:

enter image description here

UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
[myButton setImage:bgImage forState:UIControlStateSelected];
[myButton setSelected:YES];

Here is the code below(If there is something wrong with what I’m doing please tell me, Thanks):

- (void)viewDidLoad {
    UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
    [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
    // Create view
    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];

    int row = 0;
    int column = 0;
    for(int i = 0; i < _thumbs.count; ++i) {

        UIImage *thumb = [_thumbs objectAtIndex:i];
        myButton = [UIButton buttonWithType:UIButtonTypeCustom];
        myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
        [myButton setImage:thumb forState:UIControlStateNormal];
        [myButton addTarget:self 
                     action:@selector(buttonClicked:) 
           forControlEvents:UIControlEventTouchUpInside];
        myButton.tag = i; 
        NSLog(@"%i",i);

        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        NSInteger selectedImageSlot1 = [prefs integerForKey:@"selected1"];

        if ( selectedImageSlot1 == i){
            [self turnButtonToSelected];
        } 

        [view addSubview:myButton];

        if (column == 6) {
            column = 0;
            row++;
        } else {
            column++;
        }

    }

    [view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
}

- (void)turnButtonToSelected
{       UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
    [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
    [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
    [myButton setEnabled:NO];
}
- (void)highlightButton:(UIButton *)a {
    //[self turnButtonToSelected];    
}
- (IBAction)buttonClicked:(id)sender {
    myButton = (UIButton *)sender;
    self.selectedImage = [_images objectAtIndex:myButton.tag];

    if (myButton.selected) {

        [sender setSelected:NO];

    }else {

        UIImage *bgImage = [UIImage imageNamed:@"AGIPC-Checkmark-iPhone.png"];
        [sender setImage:bgImage forState:UIControlStateSelected];
        [sender setSelected:YES];
    }


    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:myButton.tag forKey:@"slot1"];

    [[NSUserDefaults standardUserDefaults] setInteger:myButton.tag forKey:@"selected1"];

    [[NSUserDefaults standardUserDefaults] synchronize];


    [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
    [self dismissModalViewControllerAnimated:NO];

}

Or is there a way to forcefully make a button enabled whenever it is disabled when tapped also?

  • 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-08T09:17:04+00:00Added an answer on June 8, 2026 at 9:17 am

    I have just tried this on a different approach.

    For me it works just fine and achieves your effect.

    Let me know if you need further explanation or the entire Xcode project.

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        //button 1
        UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        imageButton.frame = CGRectMake(30, 50, 100, 80);
        UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
        [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
        [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    
        //button 2
        UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
        imageButton2.frame = CGRectMake(180, 50, 100, 80);
        UIImage *btnImage2 = [UIImage imageNamed:@"DSC_5355.JPG"];
        [imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
        [imageButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    
        [self.view addSubview:imageButton];
        [self.view addSubview:imageButton2];
    }
    
    - (void)buttonClicked:(UIButton*)button
    {
        //NSLog(@"Button clicked! %@", button);
    
        UIImage *checkmarkImage = [UIImage imageNamed:@"check.png"];
        UIImage *empty = nil;
    
        //NSLog(@"Imageview: %d", button.imageView.tag);
        if (button.imageView.tag == 0)
        {
            [button setImage:checkmarkImage forState:UIControlStateNormal];
            button.imageView.tag = 1;
        }
        else
        {
            [button setImage:empty forState:UIControlStateNormal];
            button.imageView.tag = 0;
        }
    }
    

    Best, Chris

    Update: I have updated the code. Here is the entire ViewController. Note: I would solve this using coreData to store the current index. The following code is not the best to solve this issue but is based on your initial code!

    //
    //  SOViewController.m
    //  SOImageOverlay
    //
    //  Created by Chris on 24.07.12.
    //
    
    #import "SOViewController.h"
    
    @interface SOViewController ()
    
    @end
    
    
    #define CHECKMARK_IMAGE @"check.png"
    
    @implementation SOViewController
    
    @synthesize buttons = _buttons;
    
    #pragma mark - Custom getter
    - (NSMutableArray*)buttons
    {
        if (_buttons == nil)
            _buttons = [[NSMutableArray alloc] init];
    
        return _buttons;
    }
    
    
    #pragma mark - View lifecycle
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    //    UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
    //    UIButton *myButton = 
    //    [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    //    [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
        // Create view
    //    UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f,0.0f,1024.0f,768.0f)];
    
        int row = 0;
        int column = 0;
        for(int i = 0; i < 2; ++i) {
    
            /*UIImage *thumb = [_thumbs objectAtIndex:i];
            myButton = [UIButton buttonWithType:UIButtonTypeCustom];
            myButton.frame = CGRectMake(column*140+24, row*150+10, 100, 100);
            [myButton setImage:thumb forState:UIControlStateNormal];
            [myButton addTarget:self 
                         action:@selector(buttonClicked:) 
               forControlEvents:UIControlEventTouchUpInside];
            myButton.tag = i; 
            NSLog(@"%i",i);*/
    
            UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
            if (i == 0)
            {
                UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
                imageButton.frame = CGRectMake(30, 50, 100, 80);
                [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
            }
            else if (i == 1)
             {
                 UIImage *btnImage = [UIImage imageNamed:@"DSC_5355.JPG"];
                 imageButton.frame = CGRectMake(180, 50, 100, 80);
                 [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
             }
    
            [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            NSInteger selectedImageSlot1 = [prefs integerForKey:@"selected1"];
    
            if ( selectedImageSlot1 == i){
                [self turnButtonToSelected:imageButton];
            } 
    
            [self.view addSubview:imageButton];
    
            //adding current button into buttons array
            [self.buttons addObject:imageButton];
    
            if (column == 6) {
                column = 0;
                row++;
            } else {
                column++;
            }
    
        }
    
        //[view setContentSize:CGSizeMake(1024, (row+1) * 150 + 10)];
    
    
    //    //button 1
    //    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    imageButton.frame = CGRectMake(30, 50, 100, 80);
    //    UIImage *btnImage = [UIImage imageNamed:@"DSC_5359.JPG"];
    //    [imageButton setBackgroundImage:btnImage forState:UIControlStateNormal];
    //    [imageButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    //    
    //    //button 2
    //    UIButton *imageButton2 = [UIButton buttonWithType:UIButtonTypeCustom];
    //    imageButton2.frame = CGRectMake(180, 50, 100, 80);
    //    UIImage *btnImage2 = [UIImage imageNamed:@"DSC_5355.JPG"];
    //    [imageButton2 setBackgroundImage:btnImage2 forState:UIControlStateNormal];
    //    [imageButton2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
    //
    //    [self.view addSubview:imageButton];
    //    [self.view addSubview:imageButton2];
    }
    
    //- (void)buttonClicked:(UIButton*)button
    //{
    //    //NSLog(@"Button clicked! %@", button);
    //    
    //    UIImage *checkmarkImage = [UIImage imageNamed:@"check.png"];
    //    UIImage *empty = nil;
    //    
    //    //NSLog(@"Imageview: %d", button.imageView.tag);
    //    if (button.imageView.tag == 0)
    //    {
    //        [button setImage:checkmarkImage forState:UIControlStateNormal];
    //        button.imageView.tag = 1;
    //    }
    //    else
    //    {
    //        [button setImage:empty forState:UIControlStateNormal];
    //        button.imageView.tag = 0;
    //    }
    //}
    
    
    
    // code from stackoverflow
    // ------------------------------------------------------------
    - (void)turnButtonToSelected:(UIButton*)button
    {       
        UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
    
        //UIButton *myButton = button; //just to use the copied code
        button.selected = YES;
        [button setImage:bgImage forState:UIControlStateSelected];
    
    
    //    [myButton setBackgroundImage:bgImage forState:UIControlStateNormal];
    //    [myButton setBackgroundImage:bgImage forState:UIControlStateHighlighted];
    //    [myButton setBackgroundImage:bgImage forState:UIControlStateDisabled];
    //    [myButton setEnabled:NO];
    }
    - (void)highlightButton:(UIButton *)a {
        //[self turnButtonToSelected];    
    }
    
    - (IBAction)buttonClicked:(id)sender 
    {
        UIButton *myButton = (UIButton *)sender;
        //I have removed this line... due to the fact that I don't want to 
        //implement your arrays.
        //self.selectedImage = [_images objectAtIndex:myButton.tag];
    
        //deselecting all
        for (UIButton *button in self.buttons)
        {
            button.selected = NO;
            //[button setImage:nil forState:UIControlStateNormal];
        }
    
        if (myButton.selected) {
    
            [sender setSelected:NO];
    
        }else {
    
            UIImage *bgImage = [UIImage imageNamed:CHECKMARK_IMAGE];
            [sender setImage:bgImage forState:UIControlStateSelected];
            [sender setSelected:YES];
        }
    
    
    //    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    //    [prefs setInteger:myButton.tag forKey:@"slot1"];
    
    
        //getting index of button in array
        NSInteger index = 0;
        NSInteger foundIndex = -1;
        for (UIButton *button in self.buttons)
        {
            if (button == myButton)
            {
                foundIndex = index;
            }
            index++;
        }
    
        if (foundIndex >= 0)
        {
            [[NSUserDefaults standardUserDefaults] setInteger:foundIndex forKey:@"selected1"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    
        [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
       // [self dismissModalViewControllerAnimated:NO];
    
    }
    // ------------------------------------------------------------
    
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    
    
    - (IBAction)closeButtonClicked:(UIBarButtonItem *)sender 
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a really strange problem. I have 2 virtual machines running ubuntu 11.4
hey guys having this really simple problem but cant seem to figure out have
I'm having a really confusing problem. I have a UIWebView that is contained within
I'm having a really basic problem with NSString stringWithFormat. I want to take the
I'm really weak with math and having a problem with an image resize algorithm.
I'm having a little problem, I have a header image that is quite large
I'm really having a problem finding out how to fix this. I cannot seem
Having a really aggravating problem using Managed Metadata in SP2010 where I can get
I'm having a really weird problem with my rails app and facebook's open graph
I am having this really annoying problem on a php-site I am programming and

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.