IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)
********HWDiscoverViewController.m(发现)
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // 创建搜索框对象
- HWSearchBar *searchBar = [HWSearchBar searchBar];
- searchBar.width = ;
- searchBar.height = ;
- self.navigationItem.titleView = searchBar; //设置titleView 是搜索框
- }
HWSearchBar.m
- #import "HWSearchBar.h"
- @implementation HWSearchBar
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.font = [UIFont systemFontOfSize:];
- self.placeholder = @"请输入搜索条件"; //hit的提示信息
- self.background = [UIImage imageNamed:@"searchbar_textfield_background"];
- // 通过init来创建初始化绝大部分控件,控件都是没有尺寸
- UIImageView *searchIcon = [[UIImageView alloc] init];
- searchIcon.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
- searchIcon.width = ;
- searchIcon.height = ;
- searchIcon.contentMode = UIViewContentModeCenter; //居中
- self.leftView = searchIcon;
- self.leftViewMode = UITextFieldViewModeAlways;
- }
- return self;
- }
- + (instancetype)searchBar
- {
- return [[self alloc] init];
- }
- @end
HWSearchBar.h
- #import <UIKit/UIKit.h>
- @interface HWSearchBar : UITextField
- + (instancetype)searchBar;
- @end
**************HWHomeViewController.m(弹出下拉菜单)主页面
- #import "HWHomeViewController.h"
- #import "HWDropdownMenu.h"
- #import "HWTitleMenuViewController.h"
- @interface HWHomeViewController () <HWDropdownMenuDelegate>
- @end
- @implementation HWHomeViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- /* 设置导航栏上面的内容 */
- self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(friendSearch) image:@"navigationbar_friendsearch" highImage:@"navigationbar_friendsearch_highlighted"];
- self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(pop) image:@"navigationbar_pop" highImage:@"navigationbar_pop_highlighted"];
- /* 中间的标题按钮 */
- // UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
- UIButton *titleButton = [[UIButton alloc] init];
- titleButton.width = ;
- titleButton.height = ;
- // titleButton.backgroundColor = HWRandomColor;
- // 设置图片和文字
- [titleButton setTitle:@"首页" forState:UIControlStateNormal];
- [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- titleButton.titleLabel.font = [UIFont boldSystemFontOfSize:];
- [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
- [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateSelected];
- // titleButton.imageView.backgroundColor = [UIColor redColor];
- // titleButton.titleLabel.backgroundColor = [UIColor blueColor];
- titleButton.imageEdgeInsets = UIEdgeInsetsMake(, , , ); // 距离左边
- titleButton.titleEdgeInsets = UIEdgeInsetsMake(, , , ); // 距离右边
- // 监听标题点击
- [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
- self.navigationItem.titleView = titleButton;
- // 如果图片的某个方向上不规则,比如有突起,那么这个方向就不能拉伸
- }
- /**
- * 标题点击
- */
- - (void)titleClick:(UIButton *)titleButton
- {
- // 1.创建下拉菜单
- HWDropdownMenu *menu = [HWDropdownMenu menu];
- menu.delegate = self;
- // 2.设置内容
- HWTitleMenuViewController *vc = [[HWTitleMenuViewController alloc] init];
- vc.view.height = ;
- vc.view.width = ;
- menu.contentController = vc;
- // 3.显示
- [menu showFrom:titleButton];
- }
- - (void)friendSearch
- {
- NSLog(@"friendSearch");
- }
- - (void)pop
- {
- NSLog(@"pop");
- }
- #pragma mark - HWDropdownMenuDelegate
- /**
- * 下拉菜单被销毁了
- */
- - (void)dropdownMenuDidDismiss:(HWDropdownMenu *)menu
- {
- UIButton *titleButton = (UIButton *)self.navigationItem.titleView;
- titleButton.selected = NO;
- // 让箭头向下
- // [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
- }
- /**
- * 下拉菜单显示了
- */
- - (void)dropdownMenuDidShow:(HWDropdownMenu *)menu
- {
- UIButton *titleButton = (UIButton *)self.navigationItem.titleView;
- titleButton.selected = YES;
- // 让箭头向上
- // [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- #warning Potentially incomplete method implementation.
- // Return the number of sections.
- return ;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- #warning Incomplete method implementation.
- // Return the number of rows in the section.
- return ;
- }
- @end
********************HWDropdownMenu.m(菜单的view)
- #import "HWDropdownMenu.h"
- @interface HWDropdownMenu()
- /**
- * 将来用来显示具体内容的容器
- */
- @property (nonatomic, weak) UIImageView *containerView;
- @end
- @implementation HWDropdownMenu
- - (UIImageView *)containerView
- {
- if (!_containerView) {
- // 添加一个灰色图片控件
- UIImageView *containerView = [[UIImageView alloc] init];
- containerView.image = [UIImage imageNamed:@"popover_background"]; //黑色背景图片
- containerView.userInteractionEnabled = YES; // 开启交互
- [self addSubview:containerView];
- self.containerView = containerView;
- }
- return _containerView;
- }
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // 清除颜色
- self.backgroundColor = [UIColor clearColor];
- }
- return self;
- }
- + (instancetype)menu
- {
- return [[self alloc] init];
- }
- - (void)setContent:(UIView *)content
- {
- _content = content;
- // 调整内容的位置
- content.x = ;
- content.y = ;
- // 调整内容的宽度
- // content.width = self.containerView.width - 2 * content.x;
- // 设置灰色的高度
- self.containerView.height = CGRectGetMaxY(content.frame) + ;
- // 设置灰色的宽度
- self.containerView.width = CGRectGetMaxX(content.frame) + ;
- // 添加内容到灰色图片中
- [self.containerView addSubview:content];
- }
- - (void)setContentController:(UIViewController *)contentController
- {
- _contentController = contentController;
- self.content = contentController.view;
- }
- /**
- * 显示
- */
- - (void)showFrom:(UIView *)from
- {
- // 1.获得最上面的窗口
- UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
- // 2.添加自己到窗口上
- [window addSubview:self];
- // 3.设置尺寸
- self.frame = window.bounds;
- // 4.调整灰色图片的位置
- // 默认情况下,frame是以父控件左上角为坐标原点
- // 转换坐标系
- CGRect newFrame = [from convertRect:from.bounds toView:window];
- // CGRect newFrame = [from.superview convertRect:from.frame toView:window];
- self.containerView.centerX = CGRectGetMidX(newFrame);
- self.containerView.y = CGRectGetMaxY(newFrame);
- // 通知外界,自己显示了
- if ([self.delegate respondsToSelector:@selector(dropdownMenuDidShow:)]) {
- [self.delegate dropdownMenuDidShow:self];
- }
- }
- /**
- * 销毁
- */
- - (void)dismiss
- {
- [self removeFromSuperview];
- // 通知外界,自己被销毁了
- if ([self.delegate respondsToSelector:@selector(dropdownMenuDidDismiss:)]) {
- [self.delegate dropdownMenuDidDismiss:self];
- }
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- [self dismiss];
- }
- @end
HWDropdownMenu.h
- #import <UIKit/UIKit.h>
- @class HWDropdownMenu;
- @protocol HWDropdownMenuDelegate <NSObject>
- @optional
- - (void)dropdownMenuDidDismiss:(HWDropdownMenu *)menu;
- - (void)dropdownMenuDidShow:(HWDropdownMenu *)menu;
- @end
- @interface HWDropdownMenu : UIView
- @property (nonatomic, weak) id<HWDropdownMenuDelegate> delegate; //代理
- + (instancetype)menu;
- /**
- * 显示
- */
- - (void)showFrom:(UIView *)from;
- /**
- * 销毁
- */
- - (void)dismiss;
- /**
- * 内容
- */
- @property (nonatomic, strong) UIView *content;
- /**
- * 内容控制器
- */
- @property (nonatomic, strong) UIViewController *contentController;
- @end
*********HWTitleMenuViewController //弹出窗体里面的view
- #import "HWTitleMenuViewController.h"
- @interface HWTitleMenuViewController ()
- @end
- @implementation HWTitleMenuViewController
- - (id)initWithStyle:(UITableViewStyle)style
- {
- self = [super initWithStyle:style];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Uncomment the following line to preserve selection between presentations.
- // self.clearsSelectionOnViewWillAppear = NO;
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- // self.navigationItem.rightBarButtonItem = self.editButtonItem;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Table view data source
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return ;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *ID = @"cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
- if (!cell) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
- }
- if (indexPath.row == ) {
- cell.textLabel.text = @"好友";
- } else if (indexPath.row == ) {
- cell.textLabel.text = @"密友";
- } else if (indexPath.row == ) {
- cell.textLabel.text = @"全部";
- }
- return cell;
- }
- @end
**********HWTabBarViewController.m
- #import "HWTabBarViewController.h"
- #import "HWHomeViewController.h"
- #import "HWMessageCenterViewController.h"
- #import "HWDiscoverViewController.h"
- #import "HWProfileViewController.h"
- #import "HWNavigationController.h"
- #import "HWTabBar.h"
- @interface HWTabBarViewController () <HWTabBarDelegate>
- @end
- @implementation HWTabBarViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // 1.初始化子控制器
- HWHomeViewController *home = [[HWHomeViewController alloc] init];
- [self addChildVc:home title:@"首页" image:@"tabbar_home" selectedImage:@"tabbar_home_selected"];
- HWMessageCenterViewController *messageCenter = [[HWMessageCenterViewController alloc] init];
- [self addChildVc:messageCenter title:@"消息" image:@"tabbar_message_center" selectedImage:@"tabbar_message_center_selected"];
- HWDiscoverViewController *discover = [[HWDiscoverViewController alloc] init];
- [self addChildVc:discover title:@"发现" image:@"tabbar_discover" selectedImage:@"tabbar_discover_selected"];
- HWProfileViewController *profile = [[HWProfileViewController alloc] init];
- [self addChildVc:profile title:@"我" image:@"tabbar_profile" selectedImage:@"tabbar_profile_selected"];
- // 2.更换系统自带的tabbar
- // self.tabBar = [[HWTabBar alloc] init];
- HWTabBar *tabBar = [[HWTabBar alloc] init];
- tabBar.delegate = self; //代理
- [self setValue:tabBar forKeyPath:@"tabBar"];
- // self.tabBar = tabBar;
- // Person *p = [[Person allooc] init];
- // p.name = @"jack";
- // [p setValue:@"jack" forKeyPath:@"name"];
- }
- //- (void)viewDidAppear:(BOOL)animated
- //{
- // [super viewDidAppear:animated];
- //
- // int count = self.tabBar.subviews.count;
- // for (int i = 0; i<count; i++) {
- // UIView *child = self.tabBar.subviews[i];
- // Class class = NSClassFromString(@"UITabBarButton");
- // if ([child isKindOfClass:class]) {
- // child.width = self.tabBar.width / count;
- // }
- // }
- //}
- /**
- * 添加一个子控制器
- *
- * @param childVc 子控制器
- * @param title 标题
- * @param image 图片
- * @param selectedImage 选中的图片
- */
- - (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
- {
- // 设置子控制器的文字
- childVc.title = title; // 同时设置tabbar和navigationBar的文字 //两个一起设置
- // childVc.tabBarItem.title = title; // 设置tabbar的文字
- // childVc.navigationItem.title = title; // 设置navigationBar的文字
- // 设置子控制器的图片
- childVc.tabBarItem.image = [UIImage imageNamed:image];
- childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- // 设置文字的样式
- NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
- textAttrs[NSForegroundColorAttributeName] = HWColor(, , );
- NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
- selectTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
- [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal]; //默认的文字颜色
- [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
- // childVc.view.backgroundColor = HWRandomColor;
- // 先给外面传进来的小控制器 包装 一个导航控制器
- HWNavigationController *nav = [[HWNavigationController alloc] initWithRootViewController:childVc];
- // 添加为子控制器
- [self addChildViewController:nav];
- }
- #pragma mark - HWTabBarDelegate代理方法
- - (void)tabBarDidClickPlusButton:(HWTabBar *)tabBar
- {
- UIViewController *vc = [[UIViewController alloc] init];
- vc.view.backgroundColor = [UIColor redColor];
- [self presentViewController:vc animated:YES completion:nil];
- }
- @end
****************HWTabBar.h(自定义TabBar)
- #import "HWTabBar.h"
- @interface HWTabBar()
- @property (nonatomic, weak) UIButton *plusBtn;
- @end
- @implementation HWTabBar
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // 添加一个按钮到tabbar中
- UIButton *plusBtn = [[UIButton alloc] init];
- [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
- [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
- [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
- [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
- plusBtn.size = plusBtn.currentBackgroundImage.size;
- [plusBtn addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:plusBtn];
- self.plusBtn = plusBtn;
- }
- return self;
- }
- /**
- * 加号按钮点击
- */
- - (void)plusClick //通知代理
- {
- // 通知代理
- if ([self.delegate respondsToSelector:@selector(tabBarDidClickPlusButton:)]) {
- [self.delegate tabBarDidClickPlusButton:self];
- }
- }
- - (void)layoutSubviews
- {
- #warning [super layoutSubviews] 一定要调用
- [super layoutSubviews];
- // 1.设置加号按钮的位置
- self.plusBtn.centerX = self.width * 0.5;
- self.plusBtn.centerY = self.height * 0.5;
- // 2.设置其他tabbarButton的位置和尺寸
- CGFloat tabbarButtonW = self.width / ;
- CGFloat tabbarButtonIndex = ;
- for (UIView *child in self.subviews) {
- Class class = NSClassFromString(@"UITabBarButton");
- if ([child isKindOfClass:class]) { //如果是UITabBarButton
- // 设置宽度
- child.width = tabbarButtonW;
- // 设置x
- child.x = tabbarButtonIndex * tabbarButtonW;
- // 增加索引
- tabbarButtonIndex++;
- if (tabbarButtonIndex == ) {
- tabbarButtonIndex++;
- }
- }
- }
- // int count = self.subviews.count;
- // for (int i = 0; i<count; i++) {
- // UIView *child = self.subviews[i];
- // Class class = NSClassFromString(@"UITabBarButton");
- // if ([child isKindOfClass:class]) {
- // // 设置宽度
- // child.width = tabbarButtonW;
- // // 设置x
- // child.x = tabbarButtonIndex * tabbarButtonW;
- //
- // // 增加索引
- // tabbarButtonIndex++;
- // if (tabbarButtonIndex == 2) {
- // tabbarButtonIndex++;
- // }
- // }
- // }
- }
- @end
****************HWTabBar.h(自定义TabBar)
- #import <UIKit/UIKit.h>
- @class HWTabBar;
- #warning 因为HWTabBar继承自UITabBar,所以称为HWTabBar的代理,也必须实现UITabBar的代理协议
- @protocol HWTabBarDelegate <UITabBarDelegate>
- @optional
- - (void)tabBarDidClickPlusButton:(HWTabBar *)tabBar;
- @end
- @interface HWTabBar : UITabBar
- @property (nonatomic, weak) id<HWTabBarDelegate> delegate;
- @end
IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)的更多相关文章
- 【android开发】使用PopupWindow实现页面点击顶部弹出下拉菜单
没有太多花样,也没有很复杂的技术,就是简单的PopupWindow的使用,可以实现点击弹出一个自定义的view,view里可以随便设计,常用的可以放一个listview. demo中我只是一个点击展示 ...
- 带搜索框的jQuery下拉框插件
由于下拉框的条数有几十个,于是打算找一个可以搜索查找功能的下拉框,刚开始在网上看了几个,都是有浏览器兼容性问题,后来看到这个“带搜索框的jQuery下拉框美化插件 searchable”,看演示代码简 ...
- bootstrap日期控件在火狐下的模态框中选择时间下拉菜单无效的解决办法
今天收到程序组提交的一个兼容BUG,在火狐中使用模态框加载日期控件时选择时间下拉菜单没有效果(不能点击),而在谷歌中却是好的, 排错思路:1,在当前页面主层放置一个时间控件,测试通过 2,在ajax加 ...
- selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等
selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...
- 带搜索框的select下拉框
利用select2制作带有搜索功能的select下拉框 1.引入线上css和js <link href="https://cdnjs.cloudflare.com/ajax/libs/ ...
- 有序无序Ul->Li Ol->Li菜单,默认点击当前弹出下拉,再次点击收起下拉菜单(变形2 ---修饰)
从上面可以看出,两个问题,第一:下拉出现的太快太突然,第二:再点击下一个下拉菜单的时候,上一个不会闭合,针对这两个问题,接下来会一 一解决. 解决下拉太快: js中有个jquery效果,有一个效果是j ...
- easyui combobox点击输入框弹出下拉框
由于easyui combobox需要点击下拉箭头才能下拉,不能像select标签那样点击输入框就下拉,所以觉得不太方便,查看了一下,combobox弹出框是一个div,原本想在他的输入框的点击事件中 ...
- JQuery autocomplete获得焦点触发弹出下拉框
需求:autocomplete控件,当点击获得焦点的时候也要弹出下拉列表(autocomplete默认是输入之后才会跟随出下拉列表),下面直接贴代码. js代码: $("#customerN ...
- 用mobiscroll.js的treelist实现弹出下拉效果
首先跟上次说的一样, 第一步:引入js.css样式 1)mobiscroll-2.13.2.full.min.css 2)jquery.min.js 3)mobiscroll-2.13.2.full. ...
随机推荐
- js如何求一组数中的极值
这是一个很简单的问题,现在我们从循环开始,例如一组数[5,2,1,3,4];求其中的最大值,那么首先我们要定义一个max的中间变量,遍历数组,当遇到比max值大则赋值给max,直到循环结束,就能获取这 ...
- C#做上位机软件——绘图并传输给下位机
拿到任务之后首先分成了几个部分: 1.绘图.学习了GDI+ 2.图片保存. 3.将图片转换成byte[].由于使用Socket通信,只能传输byte[]数据,所以这一步是向下位机传输的关键. 相应地, ...
- ubuntu pycharm 无法 lock from launcher 问题解决
ubuntu pycharm 无法 lock from launcher 问题解决 最近在自己电脑上安装了python的IDE pycharm, 发现在dash也无法搜索到pycharm的启动图标.( ...
- C++ Tips and Tricks
整理了下在C++工程代码中遇到的技巧与建议. 0x00 巧用宏定义. 经常看见程序员用 enum 值,打印调试信息的时候又想打印数字对应的字符意思.见过有人写这样的代码 if(today == MON ...
- ASP.NET Boilerplate终于发布v1.0了
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:ABP经过2年多的开发,终于发布第一个主要版本了,谨此提醒ABP的使用者. ASP.N ...
- 私有无线传感网 PWSN HLINK
私有无线传感网,我把其叫做 Personal Wireless Sensor Network.此种网络最另众人所知的就是ZIGBEE了.由于在用户不同的使用场景中,对传感网络有许多不同的要求,例如:通 ...
- ip二进制计算,与运算算网段
每8位二进制,各位从左到右对应的权值分别是 128,64,32,16, 8,4,2,1 .(即2的n-1次方,n是从右到左当前位的位数) 所以随便拿一个256以内的数给你化为二进制,都可以分解为权值 ...
- 使用TextUtils.isEmpty简单化代码
如果让你判断一个文本框是否为空(null)或者没有任何值(length=0),你会怎么怎样去写代码,很多初学者可能会这样写: if(text==null || text.length==0) {... ...
- 微信小程序火车票查询 直取12306数据
最终效果图: 样式丑哭了,我毕竟不是前端,宗旨就是练练手,体验微信小程序的开发,以最直接的方式获取12306数据查询火车票. 目录结构: search1是出发站列表,search2是目的站列表,命名没 ...
- liunux 修改hostname
最近鼓捣Oracle,记录些技巧 修改hostname # vim /ect/hosts # vim /etc/sysconfig/network 修改hostname # service netwo ...