专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

搭建发微博界面导航栏

  • 目标: 点击撰写按钮弹出发微博界面,并且设置导航栏
  • Module 模块新建 Compose 文件夹
  • Compose 新建 HMComposeViewController 控制器,并勾选 Also create XIB file
  • HMMainTabbarController 的撰写微博按钮点击回调方法 composeClosure 中创建 发微博控制器 并且 Modal 出来

    class HMMainTabbarController: UITabBarController {
    
      override func viewDidLoad() {
          super.viewDidLoad()
    
          // 自定义tabbar,是只读的,不能直接使用 = 进行赋值
          // KVC 进行替换
          let newTabBar = HMMainTabBar()
          setValue(newTabBar, forKey: "tabBar")
    
          // 准备闭包
          newTabBar.composeClosure = { () -> () in
              if HMUserAccountViewModel.shared.userLogin {
                  let composeVC = HMComposeViewController()
    
                  self.present(UINavigationController(rootViewController: composeVC), animated: true, completion: nil)
              } else {
                  let oauthVC = HMOauthViewController()
    
                  self.present(UINavigationController(rootViewController: oauthVC), animated: true, completion: nil)
              }
          }
    
          ...
      }
    }
    
  • 定义 setNavigationBar 方法.实现 setNavigationBar 方法 设置导航栏

    /// 设置导航栏
    private func setNavigationBar() {
      // 导航栏左边按钮
      navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.plain, target: self, action: #selector(close))
    
      // 导航栏右边按钮
      navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
      // 发布按钮一开始不可用
      sendButton.isEnabled = false
    }
    
    // MARK: - 按钮点击事件
    func close() -> Void {
      print("close")
    }
    
    func sendStatus() -> Void {
      print("sendStatus")
    }
    
    // MARK: - 懒加载
    private lazy var sendButton: UIButton = {
      let button = UIButton()
    
      button.setTitle("发布", for: UIControlState.normal)
      button.setBackgroundImage(UIImage(named: "common_button_orange"), for: UIControlState.normal)
      button.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), for: UIControlState.highlighted)
      button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.disabled)
    
      button.frame = CGRect(x: 0, y: 0, width: 45, height: 35)
      button.addTarget(self, action: #selector(sendStatus), for: UIControlEvents.touchUpInside)
    
      return button
    }()
    
  • 实现 setNavTitle 方法 设置导航栏标题

    /// 设置导航栏标题
    private func setNavTitle() {
      // 前缀
      let prefix = "发微博"
    
      // 判断是否有用户名称.
      if let name = HMUserAccountViewModel.shared.userAccount?.screen_name {
          // 有用户名称,拼接用户名称. \n 表示换行
          let titleText = prefix + "\n" + name
    
          // 创建label
          let label = UILabel()
    
          // 显示多行
          label.numberOfLines = 0
          // 默认左对齐,改成居中对齐
          label.textAlignment = NSTextAlignment.center
          // 设置内容
          label.text = titleText
          // 设置大小
          label.sizeToFit()
    
          navigationItem.titleView = label
      } else {
          // 没有用户名称,直接显示 发微博
          navigationItem.title = prefix
      }
    }
    
  • 导航栏文本添加属性

    /// 设置导航栏标题
    private func setNavTitle() {
      // 前缀
      let prefix = "发微博"
    
      // 判断是否有用户名称.
      if let name = HMUserAccountViewModel.shared.userAccount?.screen_name {
          // 有用户名称,拼接用户名称. \n 表示换行
          let titleText = prefix + "\n" + name
    
          // 创建label
          let label = UILabel()
    
          // 显示多行
          label.numberOfLines = 0
          // 默认左对齐,改成居中对齐
          label.textAlignment = NSTextAlignment.center
    
          // 创建属性文本
          let attributeText = NSMutableAttributedString(string: titleText)
          // name: 属性名称
          // value: 属性的值
          // range: 哪些范围添加属性
          // 用户名称添加属性
          let nameRange = (titleText as NSString).range(of: name)
    
          attributeText.addAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 11), NSForegroundColorAttributeName: UIColor.lightGray], range: nameRange)
    
          // 设置属性文本
          label.attributedText = attributeText
    
          // 设置大小
          label.sizeToFit()
    
          navigationItem.titleView = label
      } else {
          // 没有用户名称,直接显示 发微博
          navigationItem.title = prefix
      }
    }
    

results matching ""

    No results matching ""