在做UIView的透明效果时,想让UIView上的图片不透明,那么就只能设置backgroundcolor = [uicolor clearcolor],
切记不可以设置 alpha = ...; 因为设置alpha的话就相当于把UIView下的subviews也都设置成透明的了
下面记录下我的这种效果的实现方法, 以免过几天就忘了,我的烂记性:
分别是横条带透明度的长条栏,四个button及四个点击后的变蓝色的图,实现方法:
customToolbar = [[UIViewalloc]initWithFrame:CGRectMake(0,367,320,49)];
// customToolbar.alpha = 0.5; //切记这个地方不要设置alpha
customToolbar.backgroundColor = [UIColorclearColor];
UIImageView *imgview = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"[email protected]"]];
imgview.frame = CGRectMake(0,0,320, 49);
[customToolbar addSubview:imgview];
(注意:后边的四个button再定义后,用 customToolbar 直接 addsubview可以实现点击,而用 imgview addsubview的话却不可以 ,不知道 为什么)
UIButton *cusbutton1 = [UIButton buttonWithType:UIButtonTypeCustom];
cusbutton1.frame = CGRectMake(20, 5, 40, 39);
cusbutton1.tag = 1;
[cusbutton1 addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
[cusbutton1 setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
[cusbutton1 setImage:[UIImage imageNamed:@"refresh_click.png"] forState:UIControlStateSelected];
// [cusbutton1 setBackgroundImage:[UIImage imageNamed:@"enjoy.png"] forState:UIControlStateNormal];
// [cusbutton1 setBackgroundImage:[UIImage imageNamed:@"butImg.png"] forState:UIControlStateSelected];
[customToolbar addSubview:cusbutton1]; //chenyy 注意这个地方,若用[imgview addsubview:cusbutton1]却不行
右下角那个小图标的实现代码:(butImg.png即是让美工就是做好的带透明度的图)
UIButton *rightButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
rightButton.frame = CGRectMake(270,350, 35, 35);
rightButton.backgroundColor = [UIColorclearColor];
// rightButton.alpha = 0.5;
[rightButton addTarget:selfaction:@selector(clickRightButton)forControlEvents:UIControlEventTouchUpInside];
[rightButton setBackgroundImage:[UIImageimageNamed:@"butImg.png"]forState:UIControlStateNormal];
[self.view addSubview:rightButton];