效果如图:
然而做出这样的效果并不顺利, 刚开始的时候代码如下:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem]; backButton.backgroundColor = color; backButton.frame = CGRectMake(18, 610, 380, 50); [backButton setTitle:@"返回登录" forState:UIControlStateNormal]; [backButton setTitleColor:color forState:UIControlStateNormal]; backButton.alpha = 0.3; backButton.layer.cornerRadius = 5; backButton.layer.masksToBounds = YES; [backButton addTarget:self action:@selector(backLogin:) forControlEvents:UIControlEventTouchUpInside];
执行之后的效果是下面这个样子的:--
对此, 我意识到是设置button透明度的时候, button上字体的透明度也会做出同样的改变. 于是出现上面的情况..
改正后的代码如下:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem]; backButton.backgroundColor = [UIColor colorWithRed:0 / 255.0 green:180 / 255.0 blue:140 / 255.0 alpha:0.3]; backButton.frame = CGRectMake(18, 610, 380, 50); [backButton setTitle:@"返回登录" forState:UIControlStateNormal]; [backButton setTitleColor:color forState:UIControlStateNormal];
如代码所示, 将透明度的设置放在backgroundcolor 中, 而不要改变button的alpha属性.执行后的样子就是第一张图显示的样子.
警纪