iOS应用中使用Toolbar工具栏方式切换视图的方法详解汇编Word文件下载.docx
- 文档编号:15309712
- 上传时间:2022-10-29
- 格式:DOCX
- 页数:14
- 大小:1.05MB
iOS应用中使用Toolbar工具栏方式切换视图的方法详解汇编Word文件下载.docx
《iOS应用中使用Toolbar工具栏方式切换视图的方法详解汇编Word文件下载.docx》由会员分享,可在线阅读,更多相关《iOS应用中使用Toolbar工具栏方式切换视图的方法详解汇编Word文件下载.docx(14页珍藏版)》请在冰豆网上搜索。
6
typedefNS_ENUM(NSInteger,UIBarStyle){
UIBarStyleDefault=0,//默认风格,蓝色文字
UIBarStyleBlack=1,//黑色背景,褐色文字
UIBarStyleBlackOpaque=1,//纯黑色背景,白色文字
UIBarStyleBlackTranslucent=2,//透明黑色背景,白色文字
};
属性:
@property(nonatomic)UIBarStylebarStyle;
//工具栏风格,默认为蓝色
@property(nonatomic,copy)NSArray*items;
//工具栏中的按钮单元,UIBarButtonItem
@property(nonatomic,assign,getter=isTranslucent)BOOLtranslucent//是否透明
@property(nonatomic,retain)UIColor*tintColor;
//按钮颜色
@property(nonatomic,retain)UIColor*barTintColor;
//工具栏颜色
方法:
※设置工具栏中的按钮单元
-(void)setItems:
(NSArray*)itemsanimated:
(BOOL)animated;
※设置工具栏的背景图像
复制代码代码如下:
-(void)setBackgroundImage:
(UIImage*)backgroundImageforToolbarPosition:
(UIBarPosition)topOrBottombarMetrics:
(UIBarMetrics)barMetrics;
※获取工具栏的背景图像
-(UIImage*)backgroundImageForToolbarPosition:
※设置工具栏的阴影图像
-(void)setShadowImage:
(UIImage*)shadowImageforToolbarPosition:
(UIBarPosition)topOrBottom;
※获取工具栏的阴影图像
-(UIImage*)shadowImageForToolbarPosition:
(UIBarPosition)topOrBottom;
ToolBar方式切换视图
1、创建工程:
运行Xcode,新建一个EmptyApplication,名称为MultiView,其他设置如下图:
2、创建3个ViewController:
依次选择File—New—NewFile,打开如下窗口:
找到UIViewControllersubclass并单击Next,打开下面的窗口:
输入名称RootViewController,并且保证Subclassof选择UIViewController,下面的两个选框都不选;
按照同样的步骤新建两个ViewController,名称分别是FirstViewController和SecondViewController。
建好后,在ProjectNavigation中显示文件如下:
3、为三个ViewController创建.xib文件:
在左边选UserInterface,右边选View,单击Next,在新窗口中的DeviceFamily中选择iPhone,单击Next,打开如下窗口:
输入名称RootView,单击Create,创建了一个.xib文件。
用同样的方法再创建两个.xib,名称分别是FirstView和SecondView。
4、修改AppDelegate:
4.1单击AppDelegate.h,在其中添加代码,在@interface之前添加@classRootViewController;
在@end之前添加@property(strong,nonatomic)RootViewController*rootViewController;
添加之后的代码如下:
#import<
UIKit/UIKit.h>
@classRootViewController;
@interfaceAppDelegate:
UIResponder<
UIApplicationDelegate>
@property(strong,nonatomic)UIWindow*window;
@property(strong,nonatomic)RootViewController*rootViewController;
@end
4.2单击AppDelegate.m,修改其代码。
在@implementation之前添加#import"
RootViewController.h"
,在@implementation之后添加@synthesizerootViewController;
然后修改didFinishLaunchingWithOptions方法如下:
7
8
9
10
11
12
13
14
-(BOOL)application:
(UIApplication*)applicationdidFinishLaunchingWithOptions:
(NSDictionary*)launchOptions
{
self.window=[[UIWindowalloc]initWithFrame:
[[UIScreenmainScreen]bounds]];
//Overridepointforcustomizationafterapplicationlaunch.
self.rootViewController=[[RootViewControlleralloc]initWithNibName:
@"
RootView"
bundle:
nil];
UIView*rootView=self.rootViewController.view;
CGRectrootViewFrame=rootView.frame;
rootViewFrame.origin.y+=[UIApplicationsharedApplication].statusBarFrame.size.height;
rootView.frame=rootViewFrame;
[self.windowaddSubview:
rootView];
self.window.backgroundColor=[UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
returnYES;
}
①
这行代码用于从RootView.xib文件中初始化rootViewController,注意initWithNibName:
中不要后缀名.xib
②
使得RootViewController的视图不会被状态栏挡住
5、修改RootViewController.h:
单击RootViewController.h,在其中添加两个属性和一个方法,如下:
@classFirstViewController;
@classSecondViewController;
@interfaceRootViewController:
UIViewController
@property(strong,nonatomic)FirstViewController*firstViewController;
@property(strong,nonatomic)SecondViewController*secondViewController;
-(IBAction)switchViews:
(id)sender;
6、打开RootView.xib,在坐边选择File'
sOwner,在右边打开IdentityInspector,在Class下拉菜单选择RootViewController:
这样,我们就可以从RootView.xib文件向RootViewController创建Outlet和Action了。
7、为RootView.xib添加工具栏:
打开RootView.xib,拖一个ToolBar到视图上,双击ToolBar上的按钮,修改其名称为SwitchViews:
8、添加Action映射:
选中SwitchViews按钮,按住Control,拖到File'
sOwner,松开鼠标后选择switchViews方法:
9、选择File'
sOwner,按住Control键,拖到View,松开鼠标,选择view:
10、修改RootViewController.m:
打开RootViewController.m文件,在@implementation之前添加代码:
#import"
FirstViewController.h"
SecondViewController.h"
在@implementation之后添加代码:
@synthesizefirstViewController;
@synthesizesecondViewControl
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- iOS 应用 使用 Toolbar 工具栏 方式 切换 视图 方法 详解 汇编