• UIToolbar的简单用法


    UIToolbar

    开发中经常会用到的控件之一,实现起来也很简单,与此同时我们还要知道 UIBarButtonItem 和 Fixed Space Bar Button Item,这两个小东西是在Bar上的按钮和间距,都被对象化了。

    来看代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    #import "ViewController.h"
    @interface ViewController ()
    //声明
    @property (nonatomic, strong) UIToolbar *mytoolbar;
    @end
    @implementation ViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
        //实例化
        self.mytoolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 30)];
        //添加到视图
        [self.view addSubview:self.mytoolbar];
        //选择风格,这里我们选择黑色风格
        self.mytoolbar.barStyle = UIBarStyleBlack;
        //添加按钮和按钮之间的间距,这些都被对象化了,按钮是可以实现方法的
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithTitle:@"hello" style:UIBarButtonItemStylePlain target:self action:@selector(sayhello)];
        UIBarButtonItem *fixed = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
        UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithTitle:@"bye" style:UIBarButtonItemStylePlain target:self action:@selector(saybye)];
        //实例化的UIToolbar里面有items属性,是一个数组,用来存放我们要加上去的按钮
        self.mytoolbar.items = @[item1, fixed, item2];
    }
    //点击item要实现的方法,输出hello或者bye
    - (IBAction)sayhello{
        NSLog(@"hello");
    }
    - (IBAction)saybye{
        NSLog(@"bye");
    }
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    @end
  • 相关阅读:
    45
    布里斯班初体验
    走出你的舒适区
    Homebrew- MAC上的包管理利器
    Java经典类库-Guava中的函数式编程讲解
    使用WebDriver遇到的那些坑
    CheckStyle, 强制你遵循编码规范
    利用php的register_shutdown_function来记录php的输出日志
    PHP-redis中文文档
    php中set_time_limit()函数运用
  • 原文地址:https://www.cnblogs.com/liumu/p/5277510.html
Copyright © 2020-2023  润新知