Change button title without animation?

Discussion in 'iOS Programming' started by DennisBlah, Feb 10, 2014.

  1. DennisBlah, Feb 10, 2014
    Last edited: Feb 11, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    Hey all, I'm trying to change some button title programaticly.
    Just using
    Code:
     [myButton setTitle: @"some new text" forState: UIControllStateNormal];
    
    Actually I'm using an array with the new content, cus it will contain the next value of the current index. But that works.

    Only thing is, once its doing that, it seems like its fading out, and back in with new title...

    Anyone knows how to shut this animation off ?
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    You should use setTitle:forState:, I believe. Otherwise when the button changes state, the title will change back.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    This code should be giving you a compile-time error since UIButton has no setTitle: method. It does have setTitle:forState:.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    Hm, it's also missing a title property. I could have sworn that it used to have that... was it perhaps removed from the documentation at some point, without actually being removed from the SDK?
     
  5. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    I know you also got the forState: parameter, else it wont build. I just didnt putted it in here.

    My question is about:
    Why does the setTitle, makes the button animate?
    It fade's out and fade's back in with new title.

    Howevery I'm doing the change before I unhide the view where the button sits in...
     
  6. ChristianJapan macrumors 601

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #6
  7. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    Oh cool, what else did you put in and not tell us? Is this part of an animation routine?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    That one should work.

    DennisBlah, the default behavior for the new System button in iOS 7 is to animate the title change. In order to avoid that animation, try this:

    Code:
    [UIView setAnimationsEnabled:NO];
    [myButton setTitle:@"some new text" forState:UIControlStateNormal];
    [UIView setAnimationsEnabled:YES];
    
     
  9. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #9


    Ahhh I see, thats great!! It still has an little animation, but thats because you press a button has animation.
    I'm glad the extra animation of changing title is gone now :)
    I didnt putted it back to Yes cus its not needed at all :) Thanks a lot!
     
  10. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #10
  11. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #11
    I would advise against setting animation to off globally and leaving it off. (by calling

    Code:
    [UIView setAnimationsEnabled: NO];
    and then never calling

    Code:
    [UIView setAnimationsEnabled: YES];

    You don't know what else that changes.

    In fact I tried it, and it stopped a whole range of UIView animations, many of which could be triggered by the system. So you might screw up the functioning of other parts of your application.

    Better to use code like this:

    Code:
    [UIView performWithoutAnimation: 
      ^{
        [aButton setTitle: @"new title" forState: UIControlStateNormal];
      }
    ];
    
     
  12. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #12
    Thanks for your reply. I understand what you mean by that. I will keep this in mind, but for that project there are no animations 'expected' anyways :)
    So again, thanks I will keep this in mind :)
     
  13. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #13
    You don't know what system methods are using UIView animation methods. Your navigation controllers might not draw correctly. Screen rotation animations might not draw correctly. Table view scrolling might not animate. Pushing an alert view might not work right. I don't know WHAT might malefaction, but SYSTEM behavior might change.

    Do not set the flag to do not animate and leave it there. This is very likely to break something seemingly unrelated, and mark my words, you will quite likely regret it after spending countless hours weeks or months from now, trying to figure out why some XYZ isn't behaving correctly.
     

Share This Page