iOS Tutorial Programmatic changing of button images

Discussion in 'iOS Tutorials' started by Mark Turney, Feb 14, 2015.

  1. Mark Turney

    Mark Turney Active Member Licensed User

    For my latest app, Less Note 1.0.0 (currently WFR), I wanted to show when a particular colored pen or the eraser was active and the others inactive. So, I used Photoshop to create glowing "active" buttons.

    Much of this (isn't it always the case? :rolleyes:) is gleaned from snippets of others' very generous contributions of code.

    1) Easiest to use the designer to create your base layout, including the starting and pressed button images.
    2) Use an image editor like Photoshop to create a third button style.
    3) Add the third button image to the list of files in the IDE.
    4) Here's the code I used in my app to highlight the active pen (black one in this case), and set the inactive pens to non glow versions:
    Code:
    Sub BlkBut_Click
        penColor = 
    Colors.Black
        penwidth = 
    2
        bmpGlow.Initialize(
    File.DirAssets, "BlkButton_Glow.png")
        SetBackgroundImage(BlkBut, bmpGlow, 
    0)
        bmpNoGlow.Initialize(
    File.DirAssets, "RedButton.png")
        SetBackgroundImage(RedBut, bmpNoGlow, 
    0)
        bmpNoGlow.Initialize(
    File.DirAssets, "BlueButton.png")
        SetBackgroundImage(BlueBut, bmpNoGlow, 
    0)
        bmpNoGlow.Initialize(
    File.DirAssets, "GreenButton.png")
        SetBackgroundImage(GreenBut, bmpNoGlow, 
    0)
        bmpNoGlow.Initialize(
    File.DirAssets, "EraseButton.png")
        SetBackgroundImage(eraseBut, bmpNoGlow, 
    0)
    End Sub
    Note that the manually (IE - not with Designer) added button images are located in File.DirAssets.
    5) SetBackgroundImage changes the button images (thanks Erel for this code from another thread):
    Code:
    'state: 0 - normal, 1 - highlighted, 2 - disabled
    Sub SetBackgroundImage(b As Button, bmp As Bitmap, state As Int)
        
    Dim no As NativeObject = b
        no.RunMethod(
    "setBackgroundImage:forState:"Array(bmp, state))
    End Sub
    6)That's it!
     
  2. stari

    stari Active Member Licensed User

    O, thks. I will use this.
     
    Mark Turney likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice