简体   繁体   中英

How to set the title color of button?

How can set the title color of the button when an image is placed on button.?Any one can help me out this.. My Code for the same is mentioned below:

startLoginBtn = [[UIButton alloc]initWithFrame:CGRectMake(25, 303, 267, 40)];
[startLoginBtn setImage:[UIImage imageNamed:@"SkipLogin.png"] forState:UIControlStateNormal];
[startLoginBtn setTitle:@"Start Login" forState:UIControlStateNormal];
[startLoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[startLoginBtn addTarget:self action:@selector(startLogin:) forControlEvents:UIControlEventTouchUpInside];
[startLoginBtn setEnabled:YES];
[self.view addSubview:startLoginBtn];

您不应将setImage用于按钮,而应使用-[UIButton setBackgroundImage: forState:]

[startLoginBtn setImage:[UIImage imageNamed:@"SkipLogin.png"] forState:UIControlStateNormal];

更改为

[startLoginBtn setBackgroundImage:[UIImage imageNamed:@"SkipLogin.png"] forState:UIControlStateNormal];

Also you can try this one,

    UIButton *Button = [UIButton buttonWithType:UIButtonTypeCustom];
  [ Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [Button addTarget:self action:@selector(YourMethodName:) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:Button];
    view.backgroundColor = [UIColor clearColor];

Put your image as background image.

Use following code.

startLoginBtn = [[UIButton alloc]initWithFrame:CGRectMake(25, 303, 267, 40)]; 
[startLoginBtn setBackgroundImage:[UIImage imageNamed:@"SkipLogin.png"] forState:UIControlStateNormal]; 
[startLoginBtn setTitle:@"Start Login" forState:UIControlStateNormal]; 
[startLoginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[startLoginBtn addTarget:self action:@selector(startLogin:) forControlEvents:UIControlEventTouchUpInside];
[startLoginBtn setEnabled:YES]; 
[self.view addSubview:startLoginBtn];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM