搭建透明效果的搜索框,滑到页面某个部分,导航条变颜色

第一步:

self.naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 64)];

self.naviView.backgroundColor = [UIColor clearColor];

[self.view addSubview:self.naviView];

[self.view bringSubviewToFront:self.naviView];

[self setupSubViews];


第二步:

- (void)setupSubViews{

CGFloat searchViewWidth = Screen_Width/3 *2;

//搜索背景

self.searchView = [[UIView alloc] initWithFrame:CGRectMake(10, 22, searchViewWidth, 35)];

self.searchView.layer.cornerRadius = 15;

self.searchView.layer.masksToBounds = YES;

self.searchView.backgroundColor = [UIColor whiteColor];

[self.naviView addSubview:self.searchView];

self.mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, searchViewWidth, 35)];

self.mySearchBar.placeholder = @"请输入搜索关键词";

self.mySearchBar.barTintColor = [UIColor whiteColor];

self.mySearchBar.layer.borderWidth = 1;

self.mySearchBar.layer.borderColor = [UIColor whiteColor].CGColor;

self.mySearchBar.layer.cornerRadius = 15;

self.mySearchBar.layer.masksToBounds = YES;

self.mySearchBar.backgroundColor = [UIColor clearColor];

UITextField * searchField = [self.mySearchBar valueForKey:@"_searchField"];

[searchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

[searchField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];

[self.searchView addSubview:self.mySearchBar];

//设置透明度

self.searchView.alpha = 0.8;

}

第三步:

#pragma mark - scrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

CGFloat alpha = scrollView.contentOffset.y / 100;

if (alpha>1) {

self.naviView.backgroundColor = [UIColor blueColor];

self.searchView.backgroundColor = [UIColor whiteColor];

self.searchView.layer.cornerRadius = 15.0f;

}else{

self.naviView.backgroundColor = [UIColor clearColor];

self.searchView.backgroundColor = [UIColor whiteColor];

self.searchView.layer.cornerRadius = 15.0f;

}

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    十年一品温如言1008阅读 2,008评论 0 3
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,680评论 6 30
  • 一:在ViewController中实例化MLPickerScrollView完成代理<MLPickerScrol...
    欧大帅Allen阅读 7,134评论 1 9
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,251评论 5 13
  • 按理说在上课期间老师是不能离开课堂的,可是在有些时候总有一些不负责任的老师出现空堂现象。 还没有下课,老师却不见了...
    高手如林阅读 4,311评论 1 0

友情链接更多精彩内容