Setting UIButton image results in blue button in iOS 7

后端 未结 15 1741
[愿得一人]
[愿得一人] 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 21:16

    In Swift 4, initialize your UIButton and assign uyour image Data as follows:

    let myButton = UIButton(type: .cutsom)
    myButton.setImage(UIImage(data:myImageData), for: .normal)
    
    0 讨论(0)
  • 2020-11-28 21:18

    In iOS 13 -- just set the Tint property to White, while keeping the type of the UIButton as Custom

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

    None of the given solutions were working for me. If you do not set an initial image in Storyboard, you can still change the image of the button by using setBackgroundImage.

    For your example, only a minor change is needed.

    NSURL *thumbURL2 = [NSURL URLWithString:@"http://example.com/thumbs/2.jpg"];
    NSData *thumbData2 = [NSData dataWithContentsOfURL:thumbURL2];
    UIImage *thumb2 = [UIImage imageWithData:thumbData2];
    [btn2 setBackgroundImage:thumb2 forState:UIControlStateNormal];
    [self.view addSubview:btn2];
    
    0 讨论(0)
提交回复
热议问题