2013-03-21 105 views
0

我正在使用xcode 4.6开发一个应用程序。在这里我想以编程方式添加UIButton到UIscrollview。这是我遵循的代码。UIscrollview中的自定义UIButton

UIButton *bt =[[UIButton alloc]initWithFrame:frame]; 
bt=[UIButton buttonWithType:UIButtonTypeCustom]; 
[bt setTitle:@"Custom Button" forState:UIControlStateNormal]; 
[bt addTarget:self action:@selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside]; 
bt.backgroundColor = [UIColor grayColor]; 
bt.titleLabel.textColor=[UIColor blueColor]; 
[self.mainscrollview addSubview:bt]; 
[self.mainscrollview bringSubviewToFront:bt]; 

现在的问题是,按钮消失(技术上它的textcolor变成白色)点击。我检查保持UIscrollview颜色为红色,该按钮仍然在视图中,但我不能得到它的文字颜色改变的原因,我如何撤消DIS。 基本上我想用UIbutton创建一个可点击的链接。 我知道uitextview方法(datadetectortype),但它没用,因为我想在链接和实际链接的标签中显示不同的文本。

注意:文本颜色不会变回蓝色并且只保留白色。

在此先感谢。

回答

2

试试下面的代码

UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom]; 
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0); 
[bt setTitle:@"Custom Button" forState:UIControlStateNormal]; 
[bt addTarget:self action:@selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside]; 
bt.backgroundColor = [UIColor grayColor]; 
bt.titleLabel.textColor=[UIColor blueColor]; 
[self.scrollTest addSubview:bt]; 

-(void)userTappedOnLink:(UIButton*)sender 
{ 
    NSLog(@"Test .."); 

    [self performSelector:@selector(changeBtnTextColor:) withObject:sender afterDelay:1.0]; 
} 

-(void)changeBtnTextColor:(UIButton*)btn 
{ 
    btn.titleLabel.textColor=[UIColor blueColor]; 
} 

希望它会为你工作。

+0

谢谢你的答案..这是一个gr8帮助兄弟!现在完美的作品。 – xrnd 2013-03-21 06:08:49

0

使用下面的代码:

[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 

问题是你设置标题到titleLabel并且想更改按钮名称颜色的文字颜色。

所有最好的!

0

check Button's fram ..是否有效? 并从您的代码中删除bt=[UIButton buttonWithType:UIButtonTypeCustom];行。下面的代码

2

使用你会得到你的解决方案

UIButton *bt =[[UIButton alloc]initWithFrame:frame]; 
bt=[UIButton buttonWithType:UIButtonTypeCustom]; 
[bt setTitle:@"Custom Button" forState:UIControlStateNormal]; 
[bt addTarget:self action:@selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside]; 
[bt setBackgroundColor:[UIColor grayColor]]; 
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 
[self.mainscrollview addSubview:bt]; 
[self.mainscrollview bringSubviewToFront:bt]; 
+0

那么,你的代码工作正常,只要我保持按钮正常,而不是自定义。但是现在,当我点击按钮时,该事件正在被调用,但没有视觉上的按钮被点击。 – xrnd 2013-03-21 06:06:03

+0

然后设置其他颜色的标题为UIControlStateHighlighted Like [bt setTitleColor:[UIColor redColor] UIControlStateHighlighted]; – Pratik 2013-03-21 06:32:20

+0

在这种情况下,颜色始终保持蓝色。但我再次改变了该按钮的被调用函数中的颜色,并且在按下按钮时产生了点击效果。 – xrnd 2013-03-21 06:37:26