好还是买现在的苹果6sp能买吗好

  NavigationBar是很常用的一个元素,所以常常需要进行自定义操作,而一种比较直观的方式就是,先定义一个类NavigationBar继承自UINavigationBar,而这个NavigationBar的内部内容则是比较复杂的了,里面添加我们需要的所有的navigationBar 的样式,而且使得这些view的尺寸都是整个NavigationBar的尺寸,然后再进行内部view的构建就可以了,而需要进行切换的时候就进行这些view之间的显示和隐藏的切换就可以了,最好使用一些动画,前面的文中有提供动画流畅的保证方式,现在新建一个HomeNavigationController 继承自UINavigationController,并且在这个类的实现中这样操作
@implementation HomeNavigationController
-(instancetype)init
self = [super initWithNavigationBarClass:[Navigationbar class] toolbarClass:nil];
if (self) {
self.delegate =
这样的话就将HomeNavigationController的导航栏的样式进行了确定了。而为了能进行子控制器的导航栏的正确显示,最好在HomeNavigationController中声明一个
@protocol SecretPresentableViewController &NSObject&
- (void)willPresentWithNavigationBar:(Navigationbar *)navigationB
而那些想要拥有这个导航栏的自控制器只需要实现协议就可以了,而NavigationBar中的多个view的左右按钮以及中间都可以完全的自定义了,而事件 的传递则使用block的方式,这里提供其中一个自定义view的h定义方式
@interface HomeNavigationView : UIView
@property(nonatomic, copy) void (^didTapComposeBlock)(void);
@property(nonatomic, copy) void (^didTapNotificationsBlock)(void);
@property(nonatomic, copy) void (^didTapChatBlock)(void);
@property(nonatomic, copy) void (^didTapScrollToTopBlock)(void);
@property(nonatomic, copy) void (^didTapNewThemeTopBlock)(void);
@property (nonatomic, strong) UILabel* titleL
@property (nonatomic, strong) UIButton *tipsNewThemeB
@property (nonatomic, strong) UIButton *unreadLeftV;
而在m文件中只要对按钮添加监听就可以了,例子是
#pragma private
-(void)left:(id)sender
if (_didTapNotificationsBlock) _didTapNotificationsBlock();
在拥有navigationBar 的控制器中对具体的block进行处理
navigationBar.homeNavigationView.didTapNotificationsBlock = ^{
[self left:nil];
而里面使用的这个left:方法则正是这个控制器中的具体的处理方法(push,modal,或者按钮的消失,lable的隐藏等等的点击相应等等),这样的话就实现了自定义bar的同时而且实现了bar和控制器之间的无缝衔接。更加方便的进行自定义的操作,但同时也可以完全实现系统提供bar的所有功能等等,而这个具体的实现过程很有可能就是apple自己内部的实现方式,因为apple很推荐我们使用block而这种bar和viewcontrol的传递消息的方式感觉这是最优秀的了。
阅读(...) 评论()现在的位置:
Navigation Bar的简单设置
关键词:无 ┊ 来源:
屏幕上方出现的工具栏就是Navigation Bar,而所谓UINavigationItem就可以理解为Navigation Bar中的内容,通过编辑UINavigationItem,我们可以使得在Navigation Bar中显示想要的东西,比如设置标题、添加按钮等。这篇博客将会以一个小例子来演示如何设置UINavigationItem。现在我用的是Xcode 4.3,在使用上跟Xcode 4.2差不多。1、首先运行Xcode 4.3,创建一个Single View Application,名称为UINavigationItem Test:2、其次,我们要使得程序运行时能够显示Navigation Bar:2.1 单击AppDelegate.h,向其中添加属性:@property (strong, nonatomic) UINavigationController *navC2.2 打开AppDelegate.m,在@synthesize viewController = _viewC之后添加代码:@synthesize navC
#pragma mark -
#pragma mark Application lifecycle2.3 修改didFinishLaunchingWithOptions方法代码如下:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES;
}此时运行程序,会发现出现了Navigation Bar:下面讲一下关于NavigationItem的简单设置。3、设置标题:打开ViewController.m,在viewDidLoad方法中[super viewDidLoad];之后添加代码:self.navigationItem.title = @"标题";运行:4、自定义标题,设置titleView:如果我们想改变标题的颜色和字体,就需要自己定义一个UILabel,并且已经设置好这个Label的内容,可以设置自己想要的字体、大小和颜色等。然后执行self.navigationItem.titleView = myL就可以看到想要的效果。4.1 打开ViewController.h,向其中添加属性:@property (strong, nonatomic) UILabel *titleL4.2 打开ViewController.m,在@implementation ViewController下面一行添加代码:@synthesize titleL4.3 在viewDidLoad方法中,去掉self.navigationItem.title = @"标题";,并添加代码://自定义标题
titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
//设置Label背景透明
titleLabel.font = [UIFont boldSystemFontOfSize:20];
//设置文本字体与大小
titleLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(255.0 / 255.0) blue:(0.0 / 255.0) alpha:1];
//设置文本颜色
titleLabel.textAlignment = UITextAlignmentC
titleLabel.text = @"自定义标题";
//设置标题
self.navigationItem.titleView = self.titleL运行:实际上,不仅仅可以将titleView设置成Label,只要是UIView的对象都可以设为titleView,例如,将4.3中的代码改成:UIButton *button = [UIButtonbuttonWithType: UIButtonTypeRoundedRect];
[button setTitle: @"按钮" forState: UIControlStateNormal];
[button sizeToFit];
self.navigationItem.titleView =则运行起来效果如下:5、为Navigation Bar添加左按钮以下是进行leftBarButtonItem设置的代码:self.navigationItem.leftBarButtonItem = (UIBarButtonItem *)
self.navigationItem.leftBarButtonItems = (UIBarButtonItem *)
self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *)
self.navigationItemsetLeftBarButtonItem:(UIBarButtonItem *) animated:(BOOL)
self.navigationItemsetLeftBarButtonItems:(NSArray *)
self.navigationItemsetLeftBarButtonItems:(NSArray *) animated:(BOOL)其实很简单,只要定义好一个UIBarButtonItem,然后执行上述某行代码就行了。5.1 为了使得运行时不出错,我们在ViewController.m中添加一个空方法,由将要创建的左右按钮使用://空方法
-(void)myAction {
}5.2 添加一个左按钮:在ViewDidLoad方法最后添加代码://添加左按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
initWithTitle:@"左按钮"
style:UIBarButtonItemStylePlain
target:self
action:@selector(myAction)];
[self.navigationItem setLeftBarButtonItem:leftButton];运行效果如下:创建一个UIBarButtonItem用的方法主要有:[UIBarButtonItemalloc]initWithTitle:(NSString *) style:(UIBarButtonItemStyle) target:(id) action:(SEL)
[UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)在第一个方法中,我们可以使用的按钮样式有:UIBarButtonItemStyleBordered
UIBarButtonItemStyleDone
UIBarButtonItemStylePlain效果分别如下:& & && & &看上去第一个和第三个样式效果是一样的。6、添加一个右按钮在ViewDidLoad方法最后添加代码://添加右按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemUndo
target:self
action:@selector(myAction)];
self.navigationItem.rightBarButtonItem = rightB运行如下:这里创建UIBarButtonItem用的方法是[UIBarButtonItemalloc]initWithBarButtonSystemItem:(UIBarButtonSystemItem) target:(id) action:(SEL)用了系统自带的按钮样式,这些样式的标签和效果如下:
& & & & & & & & 标签& & & 效果& & & & & & & & & & & &标签& & & & &效果UIBarButtonSystemItemAction&
&&& & & & UIBarButtonSystemItemPause&
& & &&UIBarButtonSystemItemAdd&
&&& & & & UIBarButtonSystemItemPlay&
& & &&UIBarButtonSystemItemBookmarks&
&&& & & & UIBarButtonSystemItemRedo&
& & &&UIBarButtonSystemItemCamera&
&&& & & & UIBarButtonSystemItemRefresh&
& & &&UIBarButtonSystemItemCancel&
&&& & & & UIBarButtonSystemItemReply&
& & &&UIBarButtonSystemItemCompose&
&&& & & & UIBarButtonSystemItemRewind&
& & &&UIBarButtonSystemItemDone&
&&& & & & UIBarButtonSystemItemSave&
& & &&UIBarButtonSystemItemEdit&
&&& & & & UIBarButtonSystemItemSearch&
& & &&UIBarButtonSystemItemFastForward&
&&& & & & UIBarButtonSystemItemStop&
& & &&UIBarButtonSystemItemOrganize&
&&& & & & UIBarButtonSystemItemTrash&
& & &&UIBarButtonSystemItemPageCurl&
&&& & & & UIBarButtonSystemItemUndo&
注意,UIBarButtonSystemItemPageCurl只能在Tool Bar上显示。7、添加多个右按钮在ViewDidLoad方法中最后添加代码://添加多个右按钮
UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(myAction)];
UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
UIBarButtonItem *rightButton3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:@selector(myAction)];
UIBarButtonItem *rightButton4 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UIBarButtonItem *rightButton5 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize
target:self
action:@selector(myAction)];
NSArray *buttonArray = [[NSArray alloc]
initWithObjects:rightButton1,rightButton2,
rightButton3,rightButton4,rightButton5, nil];
self.navigationItem.rightBarButtonItems = buttonA为了更好的显示效果,把设置titleView以及设置leftBarButtonItem的代码注释掉,运行效果如下:上面的UIBarButtonSystemItemFixedSpace和UIBarButtonSystemItemFlexibleSpace都是系统提供的用于占位的按钮样式。8、设置Navigation Bar背景颜色在viewDidLoad方法后面添加代码://设置Navigation Bar颜色
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:(218.0/255.0) green:(228.0 / 255.0) blue:(250.0 / 255.0) alpha:1];运行如下:9、设置Navigation Bar背景图片首先将准备好作为背景的图片拖到工程中,我用的图片名称是title_bg.png。将上面的代码改成://设置Navigation Bar背景图片
UIImage *title_bg = [UIImage imageNamed:@"title_bg.png"];
//获取图片
CGSize titleSize = self.navigationController.navigationBar.bounds.
//获取Navigation Bar的位置和大小
title_bg = [self scaleToSize:title_bg size:titleSize];//设置图片的大小与Navigation Bar相同
[self.navigationController.navigationBar
setBackgroundImage:title_bg
forBarMetrics:UIBarMetricsDefault];
//设置背景之后,在ViewController.m中添加一个方法用于调整图片大小://调整图片大小
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
UIGraphicsBeginImageContext(size);
[img drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledI
暂无相关文章
发布于 1197天 10小时 41分钟前,目前已有 35507 人浏览
欢迎大家转载分享,请注明来源及链接;商业媒体转载请获得授权,谢谢合作! 1551人阅读
iOS基础(65)
日常开发中少不了用到UINavigationController,但是很多情况都要自定义NavigationBar。依稀记得自己刚开始也踩了好多坑,凑今天有空,就把想到的写下来。有时间了,考虑再把自定义TabBar写一下。
1.修改Navigationbar
navigationBar其实有三个子视图,leftBarButtonItem,rightBarButtonItem,以及titleView。
1.1 &方法一:alloc一个UINavigationBar ,并给alloc出来的NavigationBar设置button及view,与系统的NavigationBar没有着明显区别。如果在没有NavigationController管理的时候想要添加一个NavigationBar,就这方法还是可以的。。。但是添加到可以滚动的视图(如tableView)的时候,会随着视图的滚动而滚动,不建议使用,代码就不贴了。
1.2 方法二:&修改系统UINavigationBar的leftBarButtonItem,rightBarButtonItem,以及titleView
//设置左button
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@&left& style:UIBarButtonItemStyleDone target:self action:@selector(test)];
self.navigationItem.leftBarButtonItem = leftI
//设置右button  UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@&button& style:UIBarButtonItemStyleDone target:self action:@selector(test)];
  self.navigationItem.rightBarButtonItem = rightI
//修改titleView字体颜色
NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor redcolor] forKey:NSForegroundColorAttributeName];
self.navigationController.navigationBar.titleTextAttributes =
navigationBar的半透明效果,可以通过
self.navigationController.navigationBar.translucent = NO;
1.3 隐藏navigationBar,自定义View代替navigationBar
此方法简单粗暴。自定义性强,一个普通的UIView,可以在任何位置放置自己想要的任何控件,设置各种颜色。
问题在与子视图布局的时候需要注意起始Y值为64(状态栏20 +&navigationBar 44)。视图过多的时候是很让人崩溃的一件事。
self.navigationController.navigationBarHidden = NO;
self.headerView = [[[NSBundle mainBundle] loadNibNamed:@&HeaderView& owner:nil options:nil]lastObject];
[self.headerView.menuButton addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
self.headerView.frame = CGRectMake(0, 20, kScreenW, 44);
[self.view addSubview:self.headerView];
1.4 &将自定义View添加到titleView上
不隐藏navigationBar,然后将自定义的View添加到titleView上,好处在于,不用考虑坐标问题。和系统的navigationBar使用起来一样简单,还增加了自定义性。唯一不足是titleView的frame不是整个navigationBar,看起来颜色不统一,解决方法是将navigationBar背景颜色调整为和自定义View一样。
self.navigationController.navigationBarHidden = NO;
HeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@&HeaderView& owner:nil options:nil]lastObject];
headerView.frame = CGRectMake(0, 20, kScreenW, 44);
self.navigationController.navigationBar.translucent = NO;
self.navigationItem setHidesBackButton = YES;
//navigationBar颜色与headerView颜色相同
self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
self.navigationItem.titleView = headerV
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:63262次
积分:1522
积分:1522
排名:千里之外
原创:85篇
转载:35篇
评论:11条
(1)(12)(5)(1)(16)(1)(1)(43)(22)(16)(4)

我要回帖

更多关于 现在的苹果6sp能买吗 的文章

 

随机推荐