iOS实践02

  第二天了,上了一天课,软件测试、数据挖掘、概率论,晚上了才有时间捣鼓捣鼓程序。

  今天只是简单的做了一点。觉得自己思考的写不出来,只能简单的写一个过程,不像第一次写这个,少了很多思考的。

  1.完善tabbar的消息提醒,自定义提醒小图标。使用KVO动态改变badge的显示值(代码:006),新浪的那个badge的图片需要拉伸,有一个自己写的一个UIIamge的分类(主要是根据从中心拉伸图片,会写在评论里)

// 添加提醒数字按钮
2 SVBadgeButton *badgeBtn = [[SVBadgeButton alloc] init];
3 badgeBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |    UIViewAutoresizingFlexibleBottomMargin;
4 [self addSubview:badgeBtn];
5 self.badgeBtn  = badgeBtn;
 1 // 006
 2 // 设置item
 3 - (void)setItem:(UITabBarItem *)item
 4 {
 5     _item = item;
 6     
 7     // KVO 监听属性改变
 8     [item addObserver:self forKeyPath:@"badgeValue" options:0 context:nil];
 9     [item addObserver:self forKeyPath:@"title" options:0 context:nil];
10     [item addObserver:self forKeyPath:@"image" options:0 context:nil];
11     [item addObserver:self forKeyPath:@"selectedImage" options:0 context:nil];
12     
13     [self observeValueForKeyPath:nil ofObject:nil change:nil context:nil];
14 }
15 
16 - (void)dealloc
17 {
18     [self.item removeObserver:self forKeyPath:@"badgeValue"];
19     [self.item removeObserver:self forKeyPath:@"title"];
20     [self.item removeObserver:self forKeyPath:@"image"];
21     [self.item removeObserver:self forKeyPath:@"selectedImage"];
22 }
23 
24 /**
25  *  监听到某个对象的属性改变了,就会调用
26  *
27  *  @param keyPath 属性名
28  *  @param object  哪个对象的属性被改变
29  *  @param change  属性发生的改变
30  */
31 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
32 {
33     // 设置文字
34     [self setTitle:self.item.title forState:UIControlStateSelected];
35     [self setTitle:self.item.title forState:UIControlStateNormal];
36     
37     // 设置图片
38     [self setImage:self.item.image forState:UIControlStateNormal];
39     [self setImage:self.item.selectedImage forState:UIControlStateSelected];
40     
41     // 设置提醒数字
42     self.badgeBtn.badgeValue = self.item.badgeValue;
43     
44     // 设置提醒数字的位置
45     CGFloat badgeY = 5;
46     CGFloat badgeX = self.frame.size.width - self.badgeBtn.frame.size.width - 10;
47     CGRect badgeF = self.badgeBtn.frame;
48     badgeF.origin.x = badgeX;
49     badgeF.origin.y = badgeY;
50     self.badgeBtn.frame = badgeF;
51 }

  2.完善nav导航条的细节(全局属性的设置):左上右上的的按钮,需要在各自的Vc中写自己的按钮,而整个nav的主题在nav里写就好(代码:007)。

 1 // 代码007
 2 // 第一次使用这个类就会调用
 3 + (void)initialize
 4 {
 5     // 设置导航栏主题
 6     [self setupNavBar];
 7     // 设置按钮主题
 8     [self setupNavBarItem];
 9 }
10 
11 + (void)setupNavBar
12 {
13     // 取出当前的tabbar
14     UINavigationBar *bar = [UINavigationBar appearance];
15     // 设置标题属性
16     NSMutableDictionary *textDic = [NSMutableDictionary dictionary];
17     textDic[NSFontAttributeName] = [UIFont boldSystemFontOfSize:19];
18     textDic[NSForegroundColorAttributeName] = [UIColor blackColor];
19     
20     [bar setTitleTextAttributes:textDic];
21 }
22 + (void)setupNavBarItem
23 {
24     UIBarButtonItem *item = [UIBarButtonItem appearance];
25     
26     // 设置文字属性
27     NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
28     textAttrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:14];
29     textAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
30     [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
31     [item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];
32 }

  3.首页控制器的中间标题需要自定义,在那放一个可以点击的按钮。对于左右的按钮,因为新浪对于navbaritem提供了图片,所有又为UIBarButtonItem写了分类。

 1 - (void)viewDidLoad {
 2     [super viewDidLoad];
 3     [self.tabBarController.tabBar.items[0] setBadgeValue:@"99"];
 4     // 左边按钮
 5     self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_friendsearch_os7" highIcon:@"navigationbar_friendsearch_highlighted_os7" target:self action:@selector(findFriend)];
 6     
 7     // 右边按钮
 8     self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_pop_os7" highIcon:@"navigationbar_pop_highlighted_os7" target:self action:@selector(pop)];
 9     
10     // 中间按钮
11     SVTitleBtn *titleBtn = [SVTitleBtn titleButton];
12     // 图标
13     [titleBtn setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
14     // 文字
15     [titleBtn setTitle:@"Sleen" forState:UIControlStateNormal];
16     // 位置和尺寸
17     titleBtn.frame = CGRectMake(0, 0, 80, 40);
18     //    titleButton.tag = IWTitleButtonDownTag;
19     [titleBtn addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
20     self.navigationItem.titleView = titleBtn;
21 }
22 - (void)titleClick:(SVTitleBtn *)titleBtn
23 { 
24     if (titleBtn.tag == 0 ) {
25         [titleBtn setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
26         titleBtn.tag = -1;
27     } else {
28         [titleBtn setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
29         titleBtn.tag = 0;
30     }
31 }

  4.发现控制器的中间是一个搜索框,也要自己写这里就直接那之前写的那个了,觉得使用起来太简单了(代码:008)

 1 // 008
 2 - (void)viewDidLoad
 3 {
 4     [super viewDidLoad];
 5     // 初始化
 6     SLSearchBar *search = [SLSearchBar searchBar];
 7     // 设置位置
 8     search.frame = CGRectMake(0, 0, 300, 30);
 9     // 添加搜索框
10     self.navigationItem.titleView = search;
11 }

 

posted @ 2016-02-29 20:43  sleen  阅读(234)  评论(2编辑  收藏  举报