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];
  • 相关阅读:
    舵机驱动-GPIO MG995 STM32
    Ymodem协议-接收
    IAP注意事项
    stm32系统时钟配置,标准库v3.5
    FREERTOS移植(MDK 、STM32F103)
    C语言常量后缀
    回调函数
    运算符记忆口诀
    C语言函数指针
    frp 搭建内网穿透
  • 原文地址:https://www.cnblogs.com/safiri/p/4021525.html
Copyright © 2011-2022 走看看