码迷,mamicode.com
首页 > 其他好文 > 详细

toolBar

时间:2016-06-09 19:45:37      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

 

#import "ViewController.h"

#import "LhbToolBar.h"

 

@interface ViewController ()<LhbToolBarDelegate>

 

@property (weak, nonatomic) IBOutlet UITextField *textField;

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    //更换键盘 inputView就是键盘

    UIView *cusTomKeyBoardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];

    cusTomKeyBoardView.backgroundColor = [UIColor lightGrayColor];

    CGFloat btnWith = (self.view.frame.size.width-10*4)/3;

    for (NSInteger i=0; i<10; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        [btn setTitle:[NSString stringWithFormat:@"%zd",i] forState:UIControlStateNormal];

        btn.frame = CGRectMake(10+(btnWith+10)*(i/3), 10+60*(i%3),btnWith, 50);

        [btn setBackgroundColor:[UIColor orangeColor]];

        [cusTomKeyBoardView addSubview:btn];

    }

    self.textField.inputView = cusTomKeyBoardView;

    

    

    //工具条

    //实现方式一:设置键盘的inputAccessoryView(辅助条、工具条)

    UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 30)];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(accessoryViewBtnClick)];

    [accessoryView addGestureRecognizer:tap];

    //可以在这个accessoryView中放一些btn等控件,来实现工具条的功能

    accessoryView.backgroundColor = [UIColor purpleColor];

//    self.textField.inputAccessoryView = accessoryView;

    

    //实现方式二:toolBar

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];

    UIButton *oneBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    oneBtn.frame = CGRectMake(10, 5, 60, 40);

    [oneBtn setBackgroundColor:[UIColor purpleColor]];

    [oneBtn setTitle:@"下一个" forState:UIControlStateNormal];

    [oneBtn addTarget:self action:@selector(ontBtnDown) forControlEvents:UIControlEventTouchUpInside];

    [toolBar addSubview:oneBtn];

//    self.textField.inputAccessoryView = toolBar;

    

//    实现方式三:xib toolBar

    LhbToolBar *lhbToolBar = [LhbToolBar creatToolBar];

    lhbToolBar.toolBarDelegate = self;

    self.textField.inputAccessoryView = lhbToolBar;

    

 

}

#pragma mark - toolBar的代理方法

- (void)toolBarNextClick:(LhbToolBar *)toolBar

{

    NSLog(@"下一个");

}

 

- (void)toolBarUpClick:(LhbToolBar *)toolBar

{

    NSLog(@"上一个");

}

 

- (void)toolbarDoneClick:(LhbToolBar *)toolBar

{

    NSLog(@"完成");

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self.view endEditing:YES];

}

 

- (void)accessoryViewBtnClick

{

    NSLog(@"工具条被点击");

}

- (void)ontBtnDown

{

    NSLog(@"下一个被点击");

}

 

 ///继承子UIToolBar的xibtoolBar

#import <UIKit/UIKit.h>

 

@class LhbToolBar;

 

@protocol LhbToolBarDelegate <NSObject>

 

- (void)toolBarNextClick:(LhbToolBar *)toolBar;

 

- (void)toolBarUpClick:(LhbToolBar *)toolBar;

 

- (void)toolbarDoneClick:(LhbToolBar *)toolBar;

 

 

 

@end

//不想写三个代理方法,也可以写一个代理方法,把iteam搞成枚举

//typedef enum {

//    LhbToolBarIteamNext,

//    LhbToolBarIteamUp,

//    LhbToolBarIteamDone

//}LhbToolBarIteam;

 

@interface LhbToolBar : UIToolbar

 

@property (nonatomic,weak) id <LhbToolBarDelegate> toolBarDelegate;

 

+ (LhbToolBar *)creatToolBar;

 

@end

@end

 

 

 

 

 

 

 

#import "LhbToolBar.h"

 

@implementation LhbToolBar

 

- (IBAction)next:(id)sender {

    if ([self.toolBarDelegate respondsToSelector:@selector(toolBarNextClick:)]) {

        [self.toolBarDelegate toolBarNextClick:self];

    }

}

 

- (IBAction)up:(id)sender {

    if ([self.toolBarDelegate respondsToSelector:@selector(toolBarUpClick:)]) {

        [self.toolBarDelegate toolBarUpClick:self];

    }

}

- (IBAction)done:(id)sender {

    if ([self.toolBarDelegate respondsToSelector:@selector(toolbarDoneClick:)]) {

        [self.toolBarDelegate toolbarDoneClick:self];

    }

}

 

 

+ (LhbToolBar *)creatToolBar

{

    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];

}

toolBar

标签:

原文地址:http://www.cnblogs.com/lhb-iOS/p/5572606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!