iOS开发小白学习体验-11
来源:互联网 发布:java socket 长链接 编辑:程序博客网 时间:2024/05/20 17:10
UINavigationBar
navigationBar的属性和方法
UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 60, 400, 44)];// bar.backgroundColor = [UIColor redColor]; /** UIBarStyle for UINavigationBar and UIToolBar * UIBarStyleDefault = 0, * UIBarStyleBlack = 1, * UIBarStyleBlackOpaque = 1, // 不提倡. Use UIBarStyleBlack * UIBarStyleBlackTranslucent = 2, // 不提倡. Use UIBarStyleBlack and set the translucent property to YES */ // 貌似,UINavigationBar在视图上显示出来的颜色由两个颜色决定。一个是UIBarStyle另一个是backgroundColor。(目前测试出来是这样的,不确定) bar.barStyle = UIBarStyleDefault;/**< bar的类型 */ bar.delegate = self;/**< navigationbar的代理 */ /** 在iOS6和之前的版本,默认是NO. 如果barStyle设置的是UIBarStyleBlackTranslucent,那么一直是YES 在iOS7的新特性: 默认情况下是YES 你可以把值设为NO,强行设置一个不透明的背景 如果导航栏有一个自定义的背景图片,那么默认值是由图片的alpha(透明度)来决定的 如果它任意一个像素的alpha小于1.0,那么就是YES 如果你用一个bar设置setsetTranslucent:YES用一张不透明的自定义背景图像 它将应用系统不透明度小于1的图像 如果你用一个bar设置setsetTranslucent:NO用一张半透明的自定义背景图像 它将会用bar的barTintColor给定义规定一个不透明的背景图片,或者用UIBarStyleBlack 的黑色,或者用UIBarStyleDefault的白色的方式,如果barTintColor为空的话 */ [bar isTranslucent]; // 推一个navigationitem,在navigationbar中间显示item的标题 // 之前的导航栏(如果它还存在的话),会在左上角显示"后退"按钮 UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"你妹的"]; [bar pushNavigationItem:item animated:YES]; // 弹出之前我们设置的item,bar上就没有了 [bar popNavigationItemAnimated:YES]; // 只读 UINavigationItem *item2 = bar.topItem; // 只读 item2 = bar.backItem; // 存放UINavigationItem 的数组 NSLog(@"%ld",bar.items.count); // 如果animated写的是yes,那么模拟一个推或者弹出就取决于新的topItem是否已经在堆栈中 [bar setItems:[[NSArray alloc]init] animated:YES]; bar.tintColor = [UIColor redColor]; bar.barTintColor = [UIColor redColor];/**< 修改导航栏整体的颜色 */ // 设置navigationbar的背景图片,是否透明取决于BarMetrics的样式 /** UIBarMetrics 决定了样式 UIBarMetricsDefault, UIBarMetricsCompact, 透明的 UIBarMetricsDefaultPrompt = 101, // Applicable only in bars with the prompt property, such as UINavigationBar and UISearchBar UIBarMetricsCompactPrompt, UIBarMetricsLandscapePhone NS_ENUM_DEPRECATED_IOS(5_0, 8_0, "Use UIBarMetricsCompact instead") = UIBarMetricsCompact, UIBarMetricsLandscapePhonePrompt NS_ENUM_DEPRECATED_IOS(7_0, 8_0, "Use UIBarMetricsCompactPrompt") = UIBarMetricsCompactPrompt, */ [bar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault]; // 设置阴影图片 bar.shadowImage = [UIImage imageNamed:@""]; // 设置bar的title的样式(是字典) bar.titleTextAttributes = @{}; // backIndicatorImage是在back按钮旁边显示 bar.backIndicatorImage = [UIImage imageNamed:@""]; // backIndicatorTransitionMaskImage是在push或者pop的时候使用并覆盖内容 bar.backIndicatorTransitionMaskImage = [UIImage imageNamed:@""]; [self.view addSubview:bar];
nabigationBar的代理方法
#pragma mark - NavigationBar的代理方法- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item{ return YES;// 可以推,如果返回NO,不能推}// called to push. return NO not to.- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item{ // 在push动画结束的时候唤醒这个方法 NSLog(@"动画结束了,我出来");}// called at end of animation of push or immediately if not animated- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item{ return YES;// 用法和shouldPushItem方法类似}// same as push methods- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item{ // 用法和didPushItem类似 NSLog(@"pop动画结束,我出来");}
UINavigationItem
在苹果的API里UINavigationItem和UINavigationBar就像兄弟一样,分不开
UINavigationItem的一些常用属性
self.view.backgroundColor = [UIColor whiteColor]; // UINavigationItem对象在UINavigationBar中显示 // UINavigationController里不能push Item UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"hello"]; UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 44)];// bar.backgroundColor = [UIColor redColor]; [bar pushNavigationItem:item animated:YES]; item.title = @"你妹的";/**< item的标题 */// item.titleView = [[UIView alloc]init];/**< 用自定义视图来代替一个标题,水平方向,只有当这个item在堆栈的顶端的时候能显示 */ item.prompt = @"这是什么";/**< 在导航栏按钮上边的描述文本 */ item.backBarButtonItem = [[UIBarButtonItem alloc]init];/**< 返回按钮 */ item.hidesBackButton = YES; item.leftBarButtonItem = [[UIBarButtonItem alloc]init];/**< 设置左边的按钮 */ item.rightBarButtonItem = [[UIBarButtonItem alloc]init];/**< 设置右边的按钮 */ item.leftBarButtonItem = nil; item.rightBarButtonItem.customView.hidden = YES; [self.view addSubview:bar];
自定义UINavigationItem的两种方法以及相对应的隐藏方法
UIImage *searchimage=[UIImage imageNamed:@"search.png"]; UIBarButtonItem *barbtn=[[[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(searchprogram)] autoRelease]; barbtn.image=searchimage; self.navigationItem.rightBarButtonItem=barbtn;这种设置出来的item图片跟大小是固定的其隐藏方法是:在需要隐藏的时候self.navigationItem.xxxItem = nil;显示方法是重新alloc-init一次;#pragma mark 第二种: IButton*rightButton = [[UIButtonalloc]initWithFrame:CGRectMake(0,0,30,30)]; [rightButtonsetImage:[UIImageimageNamed:@"search.png"]forState:UIControlStateNormal]; [rightButtonaddTarget:selfaction:@selector(searchprogram)forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem*rightItem = [[UIBarButtonItemalloc]initWithCustomView:rightButton]; [rightButton release]; self.navigationItem.rightBarButtonItem= rightItem; [rightItem release];这种方式设计出来的item比较灵活,尤其是在 隐藏显示的时候:隐藏方法:self.navigationItem.xxxItem.customView.hidden =YES;显示方法,同上xxx.hidden = NO;这种方式更合理一些,因为不需要不断的创建/消除,所以推荐用这种方法
UIBarButtonItem
UIBarButtonItem为导航栏按钮,在导航栏的左侧和右侧,他们具有许多种不同的形状和形式
UIBarButtonItem只能放在UIToolBar或者UINavigationBar这两个类上面
UIBarButtonItem的各种创建方式
最常规的创建方法,不设置任何属性- (instancetype)init NS_DESIGNATED_INITIALIZER;用编码器创建- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;自定义设置按钮的图片,以及点击方法- (instancetype)initWithImage:(nullable UIImage *)image style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;- (instancetype)initWithImage:(nullable UIImage *)image landscapeImagePhone:(nullable UIImage *)landscapeImagePhone style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action NS_AVAILABLE_IOS(5_0); // landscapeImagePhone will be used for the bar button image when the bar has Compact or Condensed bar metrics.设置按钮的文字,并设置点击方法- (instancetype)initWithTitle:(nullable NSString *)title style:(UIBarButtonItemStyle)style target:(nullable id)target action:(nullable SEL)action;选择系统自带的样式设置按钮,并设置点击方法- (instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(nullable id)target action:(nullable SEL)action;- (instancetype)initWithCustomView:(UIView *)customView;系统自带的按钮的样式 typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) { UIBarButtonSystemItemDone,显示done文字(完成) UIBarButtonSystemItemCancel,显示cancel文字(取消) UIBarButtonSystemItemEdit,显示edit文字(编辑) UIBarButtonSystemItemSave,显示save文字(保存) UIBarButtonSystemItemAdd,加号的图标 UIBarButtonSystemItemFlexibleSpace,将两个按钮撑开最大的距离 UIBarButtonSystemItemFixedSpace,将两个按钮撑开一段距离 UIBarButtonSystemItemCompose,编辑的图标 UIBarButtonSystemItemReply,返回图标 UIBarButtonSystemItemAction,跳转按钮 UIBarButtonSystemItemOrganize,组织按钮 UIBarButtonSystemItemBookmarks,图书按钮 UIBarButtonSystemItemSearch,搜索按钮 UIBarButtonSystemItemRefresh,刷新按钮 UIBarButtonSystemItemStop,关闭按钮 UIBarButtonSystemItemCamera,相机按钮 UIBarButtonSystemItemTrash,垃圾箱按钮 UIBarButtonSystemItemPlay,播放按钮 UIBarButtonSystemItemPause,暂停按钮 UIBarButtonSystemItemRewind,快退按钮 UIBarButtonSystemItemFastForward,快进按钮 UIBarButtonSystemItemUndo NS_ENUM_AVAILABLE_IOS(3_0),显示undo文字 UIBarButtonSystemItemRedo NS_ENUM_AVAILABLE_IOS(3_0),显示redo文字 UIBarButtonSystemItemPageCurl NS_ENUM_AVAILABLE_IOS(4_0), };
UIBarButtonItem的属性
UINavigationBar *bar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)]; /** 要想将UIBarButtonItem加载在UINavigationBar上, 首先应该将UIBarButtonItem加载到UINavigationItem上, 因为UIBarButtonItem只是一个导航栏按钮 */ UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"时不我待" style:UIBarButtonItemStylePlain target:self action:@selector(hello)]; UINavigationItem *item2 = [[UINavigationItem alloc]initWithTitle:@"没错就是我"]; [bar pushNavigationItem:item2 animated:YES]; item2.rightBarButtonItem = item; /* typedef NS_ENUM(NSInteger, UIBarButtonItemStyle) { UIBarButtonItemStylePlain, UIBarButtonItemStyleBordered NS_ENUM_DEPRECATED_IOS(2_0, 8_0, "Use UIBarButtonItemStylePlain when minimum deployment target is iOS7 or later"), UIBarButtonItemStyleDone, }; */ item.style = UIBarButtonItemStylePlain;/**< 默认是UIBarButtonItemStylePlain类型的 */ item.width = 20.0;/**< 默认是0.0 */ item.tintColor = [UIColor redColor];/**< 按钮文字的颜色 */ UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPageCurl target:self action:@selector(hello)]; item2.leftBarButtonItem = item3; // 给UIBarButtonItem指定背景图片 [item setBackgroundImage:[UIImage imageNamed:@"Icon.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsCompactPrompt]; [self.view addSubview:bar];
UIToolBar
UIToolBar和UINavigationBar很像,方法也很多相似的,可以借鉴一下
/**< 总的来说,UIToolbar和UINavigationBar很像,使用的时候可以借鉴一下 */ UIToolbar *bar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-44, self.view.frame.size.width, 44)]; [self.view addSubview:bar]; bar.barStyle = UIBarStyleDefault;/**< bar的类型,barStyel只为了ToolBar和NavigationBar用 */ bar.tintColor =[UIColor redColor];/**< 按钮的明调颜色 */ bar.barTintColor = [UIColor redColor];/**< bar的明调颜色 */ UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(hello)]; UIBarButtonItem *item2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:@selector(hello)];/**< 撑开合适的距离 */ UIBarButtonItem *item3 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(hello)]; UIBarButtonItem *item4 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(hello)];/**< 撑开最大的距离 */ UIBarButtonItem *item5 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:@selector(hello)]; bar.items = [NSArray arrayWithObjects:item1,item2,item3,item4,item5, nil];/**< 这是toolbar添加UIBarButtonItem的数组,加了多少个就会在toolbar上显示多少个按钮 */
0 0
- iOS开发小白学习体验-11
- iOS开发小白学习体验-3
- iOS开发小白学习体验-4
- iOS开发小白学习体验-5
- iOS开发小白学习体验-6
- iOS开发小白学习体验-8
- iOS开发小白学习体验-9
- iOS开发小白学习体验-10
- iOS开发小白学习体验-UIWebView
- iOS开发小白学习体验-7-网络请求
- iOS开发小白学习体验-UICollectionView(一)
- iOS开发小白学习体验-CALayer的一些整理
- IOS开发学习体验
- php开发小白上手体验
- 小白学习JavaScript初体验
- php学习小体验
- ios开发初体验
- IOS开发初体验
- HDU 5489 Removed Interval (dp+线段树) 2015合肥网络赛
- iOS开发小白学习体验-UICollectionView(一)
- Java运行cmd命令并显示输出信息
- Ember.js 入门指南——loading、error子状态
- #6 LeetCode——ZigZag Conversion
- iOS开发小白学习体验-11
- iOS开发小白学习体验-CALayer的一些整理
- Swing__JComboBox 用法
- 闭包深入理解
- iOS开发小白学习体验-UIWebView
- 关于Netbeans的JTable显示数据的问题
- 推荐几款App跨平台开发工具
- grunt使用小结
- Ajax小结