2015-07-02 26 views
0

如何设置按钮的title color当图像被放置在一个button.?Any能帮助我这个.. 我对相同的代码如下所述:如何设置按钮的标题颜色?

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]; 
+0

可能的重复[我如何更改UIButton标题颜色?](http://stackoverflow.com/questions/2474289/how-can-i-change-uibutton-title-color) –

回答

0

把你的图片作为背景图片。

使用以下代码。

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]; 
+2

为什么你要求每个人都upvote你的答案? –

+0

嘿,我的意思是,不要重复同样的答案,我们可以upvote第一个正确的答案给出的问题。 –

1

你不应该使用setImage为您的按钮,只需使用-[UIButton setBackgroundImage: forState:]

+0

你试过了吗?使用图像将覆盖标题,只使用backgroundimage,它会在您的标题下。希望这可以帮助。 –

+2

当我阅读他的问题时,我没有看到你的答案,直到页面刷新。 :D –

+0

@LalitKumar我相信你今天过得很糟糕,兄弟。只需要一些excersie和睡觉。 :) –

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

改变它

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

你也可以试试这个,

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];