[iphone开发私房菜_1_] 在navigationBar上面添加多个任意控件

本文介绍如何在iOS导航栏(navigationBar)上添加不同类型的控件,包括UIBarButtonItem、UIToolbar、UISegmentedControl、UILabel和UIProgressView。通过示例代码演示了在固定位置和任意位置添加这些控件的方法,提供了丰富导航栏样式的实现技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天这道菜主要是在navigationBar上面加入任意数量的任何控件。

(转载请保留此文字:本文来源:[ [iphone开发私房菜_1_] 在navigationBar上面添加多个任意控件 http://blog.youkuaiyun.com/ipromiseu/archive/2010/12/16/6080474.aspx] write by Gray.Luo guohui.great@gmail.com)
    基本的navigationBar上面就左,中,右 3个位置,而且默认也是添加UIBarButtonItem/UINavigationBar按钮,但是很多开发过程中会遇到在上面添加更多其它控件,经过研究后,所以特写此文,算是做个笔记,也希望能够帮助朋友解决正在解决的这方面的问题。
1.在固定位置添加UIBarButtonItem
[cpp]  view plain copy
  1. UIBarButtonItem *myButton = [[[UIBarButtonItem alloc]  
  2.                 initWithTitle:@"myButton"  
  3.                 style:UIBarButtonItemStyleBordered  
  4.                 target:self   
  5.                 action:@selector(action)]autorelease];  
  6. self.navigationItem.leftBarButtonItem = myButton;  
  7. //self.navigationItem.rightBarButtonItem = myButton;  
  8. //self.navigationItem.backBarButtonItem = myButton;  
  9. [myButton release];  
 

  NavigationItem类有以下一些成员:

-title

-titleview

-backBarButtonItem//这是有返回上一级事件的后退按钮

-rightBarButtonItem

-leftBarButtonItem

 

 

2.在任意位置添加一个UIToolbar叠加到navigationBar上,然后设置其背景透明,则可以实现在上这个navigationBar 上面添加多个按钮的效果
[cpp]  view plain copy
  1. UIToolbar *mycustomToolBar;  
  2. NSMutableArray *mycustomButtons = [[NSMutableArray alloc] init];  
  3. UIBarButtonItem *myButton1 = [[[UIBarButtonItem alloc]  
  4.                 initWithTitle:@"Get5"  
  5.                 style:UIBarButtonItemStyleBordered  
  6.                 target:self   
  7.                 action:@selector(action)]autorelease];  
  8. myButton1.width = 40;  
  9. [mycustomButtons addObject: myButton1];  
  10. UIBarButtonItem *myButton2 = [[[UIBarButtonItem alloc]  
  11.                 initWithTitle:@"Play5"  
  12.                 style:UIBarButtonItemStyleBordered  
  13.                 target:self   
  14.                 action:@selector(action)]autorelease];  
  15. myButton2.width = 40;  
  16. [mycustomButtons addObject: myButton2];   
  17.   
  18. mycustomToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f,320.0f, 44.0f)];  
  19. //mycustomToolBar.center = CGPointMake(160.0f,200.0f);  
  20. mycustomToolBar.barStyle = UIBarStyleDefault;  
  21. [mycustomToolBar setItems:mycustomButtons animated:YES];  
  22. [mycustomToolBar sizeToFit];      
  23. [self.view addSubview:mycustomToolBar];  
  24. //self.navigationItem.titleView = mycustomToolBar;//与上一句都可实现在上面叠加工具条  
  25. //将toolbar的颜色设置为透明,总之使用两个控件叠加完美  
  26. [mycustomToolBar release];  
  27. [mycustomButtons release];  
 

这里是在UIToolbar 上面添加UIBarButtonItem,然而我们很多时候可能会添加其它控件,如:switch,label等等,所以在UIToolbar上面如何添加各种控件,就参考下一篇文章。

3.在任意位置添加UISegmentedControl
[cpp]  view plain copy
  1. UISegmentedControl * mySegment;  
  2. mySegment = [[UISegmentedControl alloc]  
  3.                initWithFrame:CGRectMake(5.0f, 10.0, 60.0f, 30.0f)];  
  4. [mySegment insertSegmentWithTitle:@"mySeg1" atIndex:0 animated:YES];   
  5. [get5Segment insertSegmentWithTitle:@"mySeg2" atIndex:1 animated:YES];    
  6. mySegment.segmentedControlStyle = UISegmentedControlStyleBar;  
  7. [mySegment addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];  
  8. mySegment.selectedSegmentIndex = -1;  
  9. [self.navigationController.navigationBar addSubview: mySegment];  
  10. [mySegment release];  
 
如果要在navigationBar实现多个按钮,而且某个功能块的类似按钮需要挨在一起,用segment实现还是很不错,用UIBarButtonItem实现的话,按钮间总是有一个间隔。
4.在任意位置添加UILabel
[cpp]  view plain copy
  1. UILabel* myLabel;  
  2. myLabel=[[UILabel alloc] initWithFrame:CGRectMake(100.0f, 14.0f, 100.0f, 10.0f)];  
  3. myLabel.font=[UIFont systemFontOfSize:10];  
  4. myLabel.backgroundColor = [UIColor clearColor];  
  5. [self.navigationController.navigationBar addSubview: myLabel];  
  6. [myLabel release];  
 
5.在任意位置添加UIProgressView 
[cpp]  view plain copy
  1.     UIProgressView *myProgress;  
  2. myProgress =[[UIProgressView alloc] initWithFrame:CGRectMake(80.0f, 28.0f, 150.0f, 8.0f)];  
  3. [self.navigationController.navigationBar addSubview: myProgress];  
  4. [myProgress release];     
 
小结:通过上面的方法 ,应该可以抛砖引玉,让你自己添加其他任意控件。还等什么呢?赶快试一下吧,让你的navigationBar条丰富多彩吧!(转载请保留此文字:本文来源:[ [iphone开发私房菜_1_] 在navigationBar上面添加多个任意控件 http://blog.youkuaiyun.com/ipromiseu/archive/2010/12/16/6080474.aspx] write by Gray.Luo guohui.great@gmail.com)
`QtCalendarNavigationBar` 是 Qt 库中用于与日历组件交互的一个类,它允许用户导航到不同的日期。这个部件通常与 `QCalendarWidget` 结合使用,在应用中提供了一个直观的日历视图。 ### 示例: 假设您正在创建一个应用程序,其中包含一个用于查看员工假期的日历界面。您可以使用 `QCalendarWidget` 和 `QtCalendarNavigationBar` 来实现这一功能。下面是一个简单的示例代码片段: ```python from PyQt5.QtWidgets import QApplication, QWidget, QCalendarWidget, QVBoxLayout, QtCalendarNavigationBar from PyQt5.QtCore import Qt class CalendarApp(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() # 创建日历组件 calendar = QCalendarWidget(self) layout.addWidget(calendar) # 添加导航栏 navigation_bar = QtCalendarNavigationBar(self) navigation_bar.setCalendarWidget(calendar) layout.addWidget(navigation_bar) self.setLayout(layout) self.setWindowTitle('Calendar Navigation') self.setGeometry(300, 300, 400, 300) self.show() if __name__ == '__main__': import sys app = QApplication(sys.argv) ex = CalendarApp() sys.exit(app.exec_()) ``` 在这个示例中,我们首先导入必要的 PyQt5 模块。然后定义了一个继承自 `QWidget` 的子类 `CalendarApp`,并初始化了它的布局。接着添加了 `QCalendarWidget` 组件和 `QtCalendarNavigationBar`,并将后者设置为前者(日历组件)的导航器。最后设置了窗口标题、大小和位置,并启动事件循环。 ### 相关问题: 1. 如何通过点击按钮来改变当前显示的月份? 2. `QtCalendarNavigationBar` 提供哪些具体的导航操作? 3. 如何在日历上添加或删除事件?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值