iOS中设置导航栏标题( titleView)的字体颜色和大小

在iOS中,经常会对一些导航栏titleView进行自定义,首先介绍一下对navgationBar 上的title设置的三种方法:

<1> self.title = @"我是title" ;

直接设置

<2> self.navigationItem.title = @"我是title" ;

以上两种方法 title的显示跟调用顺序有关,谁后调用显示谁

<3>  UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 62, 20)] ;

        titleLabel.text = @"我是title" ;

         self.navigationItem.titleView = titleLabel ;

   以上<3>的显示优先级是最高的  其实是<1><2>,<1><2>相互没有优先级,只跟调用顺序有关


对于titleView的字体颜色和大小 我们主要是针对于上面第三种方法进行两种方式的设置:

      <1> UILabel * titleLabel               = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 62, 20)] ;

             titleLabel.text                        = @"我是title"                                                                        ;

             titleLabel.backgroundColor  = [UIColor blueColor]                                                             ;

             titleLabel.textColor               = [UIColor whiteColor]                                                           ;

             titleLabel.font                        = [UIFont systemFontOfSize:26]                                           ;

             self.navigationItem.titleView = titleLabel                                                                             ;

       <2> [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:26],NSForegroundColorAttributeName:[UIColor whiteColor]}] ;




  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
旅游网站导航栏控制器的关键代码会根据具体实现方式不同而有所不同,下面是一个简单的示例: ```swift class NavigationBarViewController: UIViewController { // 导航栏控件 let searchBar = UISearchBar() let segmentedControl = UISegmentedControl(items: ["国内游", "出境游", "周边游"]) let filterButton = UIButton(type: .system) override func viewDidLoad() { super.viewDidLoad() // 添加导航栏控件 searchBar.placeholder = "搜索旅游目的地" navigationItem.titleView = searchBar segmentedControl.selectedSegmentIndex = 0 navigationItem.titleView = segmentedControl filterButton.setTitle("筛选", for: .normal) navigationItem.rightBarButtonItem = UIBarButtonItem(customView: filterButton) // 设置导航栏外观 navigationController?.navigationBar.tintColor = .white navigationController?.navigationBar.barTintColor = .blue navigationController?.navigationBar.isTranslucent = false navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] } // 搜索按钮点击事件 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { // 处理搜索逻辑 } // 分段控件点击事件 @objc func segmentedControlValueChanged(_ segmentedControl: UISegmentedControl) { // 处理分段逻辑 } // 筛选按钮点击事件 @objc func filterButtonTapped(_ sender: UIButton) { // 处理筛选逻辑 } } ``` 这个示例,`NavigationBarViewController` 是一个 UIViewController 子类,用于管理导航栏控件。在 `viewDidLoad()` 方法,我们添加了三个控件:`searchBar`、`segmentedControl` 和 `filterButton`。然后,我们还对导航栏外观进行了一些设置。最后,我们还添加了三个事件处理方法:`searchBarSearchButtonClicked(_:)`、`segmentedControlValueChanged(_:)` 和 `filterButtonTapped(_:)`。这些方法会根据不同的控件事件来处理对应的业务逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值