开发者

change button image in viewdidload function

开发者 https://www.devze.com 2023-02-16 08:38 出处:网络
Is it possible to change the button image in viewdidload/viewwillappear function. If possible then how the particula开发者_Go百科r button(if I have 2 buttons) will identify in these function.This is t

Is it possible to change the button image in viewdidload/viewwillappear function. If possible then how the particula开发者_Go百科r button(if I have 2 buttons) will identify in these function.


This is the code I use to update the image of weather button in my viewDidLoad:

UIImage *img = [[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:iconURL]]] retain];
if (img != nil) 
{ 
    [weatherButton setImage:img forState:normal];
    img = nil; 
}

I am getting the image from a URL. Hope this helps.


        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        button.frame = CGRectMake(275, 8, 40.0, 40.0);
        [button setBackgroundImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"button_home.png"]] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(goHome) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [self.view bringSubviewToFront:button];

Here I have created the button programmatically!


Yes, u can use this

[button setImage:image forState:state];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号