zoukankan      html  css  js  c++  java
  • UI: 使用 UIBarButtonItem 给导航栏添加按钮

    问题:

    希望将按钮添加到导航栏中 
    1.导航栏属于 UINavigationBar 类,你可以再任何时候创建它,并将它添加到任意的 view 中。 
    2.创建一个导航按钮,须要做一下工作:

       创建一个 UIBarButtonItem 实例。

      使用视图控制器的 navigationItem 属性将按钮添加到视图控制器的导航栏中,。 NavigationItem 属性允许我们与导航栏进行交互。这个属性自身有两个属性,分别为 rightBarButtonItem 和 leftBarButtonItem。这两个属性都属于 UIBarButtonItem 类。 

      代码:  

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(performAdd)];

    我们可以创建不同的系统按钮:使用 UIBarButtonItem类的 initWithBarButtonSystemItem:target:action:初始化方法来完成这项工作。 

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
    action:@selector(performAdd:)];
    类 UIBarButtonItem 中的真正最棒的初始化方法中,其中一个是 initWithCustomView: 方法。这个方法能接受任何视图。这意味着我们甚至可以将 UISwitch作为一个按钮添加到导航栏上。这似乎看起来不怎样,但是值得一试。 
    UISwitch *simpleSwitch = [[UISwitch alloc]init];
        simpleSwitch.on = YES;
        [simpleSwitch addTarget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:simpleSwitch];
  • 相关阅读:
    Makoto and a Blackboard CodeForces
    Bash Plays with Functions CodeForces
    2016 计蒜之道 初赛 第一场 D 青云的机房组网方案 (虚树)
    常用数论函数求和公式
    AC日记——元素查找 codevs 1230
    AC日记——鬼谷子的钱袋 codevs 2998
    AC日记——接龙游戏 codevs 1051
    AC日记——逆波兰表达式 openjudge 3.3 1696
    AC日记——欧几里得的游戏 洛谷 P1290
    AC日记——刺激 codevs 1958
  • 原文地址:https://www.cnblogs.com/safiri/p/4021525.html
Copyright © 2011-2022 走看看