微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

八swift 中UINavigationController 中的一些操作

1、 推拽使用UINavigationController

XCode 自动生成两个UI界面。

NativeController:继承自UINavigationController,后台swift 实现类需继承。示例代码如下:

import UIKit
class HomeNavigationController : UINavigationController {

override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

TableView(Prototype Content):继承自UITableViewController,后台swift 实现类需要继承。示例代码如下:

import UIKit
class HoMetableViewController : UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
           print("HoMetableViewController");

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }

}

2、 关联视图和后台

选择 Home Navigation Controller 视图

右侧切换到身份识别,Class 处填入 HomeNavigationController

按照HomeNavigationController 的方法,填入TableView

3、 改变UINavigationController的背景

方法1:通过界面方式

调整选项卡至 Attributes inspector (属性识别器)

Bar Tint属性: 选择颜色

方法2:后台代码
HomeNavigationController -> ViewDidLoad() {

super.viewDidLoad()

// 改变 navigationBar 颜色
self.navigationBar.barTintColor = UIColor.blackColor()
}

方法3:后台代码,变量方式
按住 control拖拽 Main.storyboard (双击打开) 内的“Home Navigation Controoler” 内的 Home Navigation Bar,至 HomoNavigationController.swift(单击打开) ,在弹出窗口的Name 处填入 homeNavigationBar
Swift代码自动生成变量 @IBOutlet weak var homeNavigationBar: UINavigationBar! (此代码自动生成


调整后运行,结果如下:

4、 改变Title(文字图片

方式1:无代码
选择视图中“Table View” 下的 UINavigationItem,并在右侧切换到“Attributes inspector” (属性识别器),
在Title 中填入“Title”。

方式2:通过代码
在 HoMetableViewController 的 viewDidLoad 函数内 加入

super.viewDidLoad()
print(“HoMetableViewController”)
// 改变标题
self.navigationItem.title = “helloTitle”

方法3:通过代码,使用label(此方式能实现仅显示文字
在HoMetableViewConroller 内新增一个方法

// title上仅含有文字
func showTitle(){

let rect = CGRect(x: 0,y: 0,width: 100,height: 30)
let label = UILabel(frame: rect)
label.text = “OA办公”
label.textColor = UIColor.whiteColor()
self.navigationItem.titleView = label
}
在 viewDidLoad 内调用 showTitle();

方法1、2、3修改Title 后运行结果如下:

方法4:通过代码图片文字共同显示
在HoMetableViewConroller 内新增一个方法

// title上含有图片文字
func showTitleImage(){

let rect = CGRectMake(0,160,30)
    let ImageView = UIImageView(frame: rect)

    let button = UIButton(type: .Custom)
    button.frame = CGRectMake(0,30)
    button.setTitle("OA办公",forState:UIControlState.normal)
    button.setimage(UIImage(named: "home-logo"),forState: .normal)

    ImageView.addSubview(button)
    self.navigationItem.titleView = ImageView

}

在 viewDidLoad 内调用 showTitleImage();
调用运行结果如下:

原文地址:https://www.jb51.cc/swift/325281.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐