Setting UIButton image results in blue button in iOS 7

后端 未结 15 1740
[愿得一人]
[愿得一人] 2020-11-28 20:52

On iOS 6 SDK I wrote the following lines of code to display an image inside a button:

NSURL *thumbURL2 = [NSURL URLWithString:@\"http://example.com/thumbs/2.         


        
相关标签:
15条回答
  • 2020-11-28 20:58

    Swift 3, 4, 5 :

    let image = UIImage(named: "my-image")
    myButton.setImage(image.withRenderingMode(.alwaysOriginal), for: .normal)
    
    0 讨论(0)
  • 2020-11-28 20:58

    The issue is the TintColor. By default, iOS throws a blue tint color over every button. You can get around it through 3 ways.

    1. Change the tint color. [button setTintColor:[UIColor blackColor]]; This may color your image in ways you don't want it to.

    2. As most other suggested, set the background image. [button setBackgroundImage:[UIImage...]];

    3. Add an UIImageView to your button.

    UIImageView * img = [[UIImageView alloc] initWithImage:[UIImage...]];

    [button addSubView:img];

    0 讨论(0)
  • 2020-11-28 21:01

    This worked for me

    [myButton1 setBackgroundImage:[UIImage imageNamed:@"phones.png"] forState:UIControlStateNormal];
    

    Note:Remove front image before doing this.

    0 讨论(0)
  • 2020-11-28 21:03

    I had the same issue. On my storyboard I had a button without any image.

    I would then assign the image in the code.

    IOS 7 came and I got a lot of blue images.

    The resolution was simple yet confusing. If I assign any image on the storyboard and then change the image at run time it works fine.

    You always must specify a starting image on the storyboard even if you are not going to use it.

    0 讨论(0)
  • 2020-11-28 21:05

    Using Xcode 9.2 none of the above solutions worked for what I was looking for.

    I was looking for a solution that will let me set .normal and .selected UIControlState images inside the storyboard for their original rendering mode, but, inside the Swift file, no string literals should exist regarding the image names.

    Basically, inside your code you will get the image you set inside your storyboard for .normal state and re-render it as .alwaysOriginal (Same for .selected state), then, you will set that image (which is now rendered as original and won't be affected by the tint) for the relevant state (.normal and .selected) of your UIButton.

    Here it is:

    // Get your .normal image (you set via your storyboard) and render it as original
    let unselectedImage = yourButton.image(for: .normal)?.withRenderingMode(.alwaysOriginal)
    // Set your normal image but this time rendered as original
    yourButton.setImage(unselectedImage, for: .normal)
    // Same for selected state
    let selectedImage = yourButton.image(for: .selected)?.withRenderingMode(.alwaysOriginal)
    yourButton.setImage(selectedImage, for: .selected)
    

    This way you can set your button image states and if the image name will change, it won't affect your code.

    0 讨论(0)
  • 2020-11-28 21:09

    For swift:

        let aButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    
    0 讨论(0)
提交回复
热议问题