UItableview、UINavigation设置

2017-04-14  本文已影响0人  蓝汐o

UITableView

TableView中取消选中颜色变化,在didSelectRowAtIndexPath中写入[tableView deselectRowAtIndePath]就可以取消选中状态。当然我们可以用dispath_after延迟选中选中。

TableView的两种类型,Plain为分隔线充满屏幕,且组头有悬浮模式。group有默认的组头,且每组开头有一个分隔线没法去除。

tableView最好使用Plain模式,设置无分割线,在cell中画分割线,取消组头悬浮模式。取消悬浮如下代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

CGFloat sectionHeaderHeight = 40;

if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {

scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);

} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {

scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);

}

}

UINavigationBar

全局NavigationBar

UINavigationBar *bar = [UINavigationBar appearance];

设置半透明效果,若是半透明的那么颜色,透明度都会有系统偏移,有半透明效果。建议设置为no。

self.navigationController.navigationBar.translucent = NO;

设置背景色

self.navigationController.navigationBar.barTintColor = [UIColor redColor];

设置背景图

UINavigationBar *bar = [UINavigationBar appearance];

[bar setBackgroundImage:[UIImage imageNamed:@"alert_error_icon"] forBarMetrics:UIBarMetricsDefault];

设置镂空颜色

UINavigationBar *bar = [UINavigationBar appearance];

[bar setTintColor:[UIColor grayColor]];

UINavigationItem

设置镂空色

//    UITextAttributeFont - 字体

//    UITextAttributeTextColor - 文字颜色

//    UITextAttributeTextShadowColor - 文字阴影颜色

//    UITextAttributeTextShadowOffset - 偏移用于文本阴影

UINavigationBar *bar = [UINavigationBar appearance];

[bar setTintColor:[UIColor grayColor]];

//或者

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];

self.title=[NSString stringWithFormat:@"第%lu页",(unsigned long)self.navigationController.viewControllers.count];

自定义颜色,使用tintcolor必须要是镂空图,不然会被着色成为一个颜色。所以建议使用显示原图片着色模式,代码如下:

//自定义试图

UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithCustomView:button];

//或者改变图片作色模式

UIImage *image = [UIImage imageNamed:@"add"];

UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStyleDone target:nil action:nil];

设置返回按钮

说一下使用pushViewController切换到下一个视图时,navigation controller按照以下3条顺序更改导航栏的左侧按钮(本段摘自网络):

1、如果B视图有一个自定义的左侧按钮(leftBarButtonItem),则会显示这个自定义按钮;

2、如果B没有自定义按钮,但是A视图的backBarButtonItem属性有自定义项,则显示这个自定义项;

3、如果前2条都没有,则默认显示一个后退按钮,后退按钮的标题是A视图的标题;

设置title的偏移值,常用来隐藏title。但是多push几个页面可能造成ViewControll的Title位置偏移有隐患。一般跳到第三个试图的时候就会影响控制器的title位置了。

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)

forBarMetrics:UIBarMetricsDefault];

设置背景图

UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

设置NavigationItem的位置

+(void)createBarButtonItemTitle:(NSString*)title andImageName:(NSString*)imageName andSEL:(SEL)sel onViewController:(UIViewController*)viewController  andIsLeft:(BOOL)isLeft{

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

button.frame = CGRectMake(10, 0, 40, 40);

[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

button.layer.cornerRadius =20;

button.layer.masksToBounds = YES;

UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]initWithCustomView:button];

//设置返回按钮的属性

UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

if (isLeft) {

negativeSeperator.width = -20;//此处修改到边界的距离,请自行测试

viewController.navigationItem.leftBarButtonItems = @[negativeSeperator,buttonItem];

}else{

negativeSeperator.width = -15;//此处修改到边界的距离,请自行测试

viewController.navigationItem.rightBarButtonItems = @[negativeSeperator,buttonItem];

}

[button addTarget:viewController action:sel forControlEvents:UIControlEventTouchUpInside];

}

自定义返回按钮

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:19.0]}];

self.title=[NSString stringWithFormat:@"第%lu页",(unsigned long)self.navigationController.viewControllers.count];

//自定义返回按钮

self.title=[NSString stringWithFormat:@"第%lu页",(unsigned long)self.navigationController.viewControllers.count];

UIImage *backButtonImage = [[UIImage imageNamed:@"fanhui.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 30, 0, 0)];

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

//将返回按钮的文字position设置不在屏幕上显示

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];

或者

//所有的子界面返回时都变成了我们定义的文字,如果不想显示文字,直接"",就会单独显示一个系统的返回箭头图标,也是很清晰的感觉。

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];

self.navigationItem.backBarButtonItem = item;

设置TitleView

低版本 这里的titleView可能会受navigationItem影响位置,建议可以在给他设置个frame。

self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"appcoda-logo.png"]];

设置title

//设置title

self.title = @"设置";

navigationItem和navigationBar如果过于复杂,可以隐藏NavigationBar,自定义View作为NavigationBar。同时如果自定义的ViewController返回按钮影响title的位置,那么就很难更改title的位置或者所修改LeftBarButtonItem的Insets值。那么建议隐藏系统的返回按钮自定义UIBarButtonItem作为左边返回按钮,但是这种方法很苦逼,每个控制器都要写,可以采用子视图控制器继承父的方法。

设置xib customView显示在sb上,使用IB_DESIGNABLE和IBInspectable。

#import

IB_DESIGNABLE

@interface CustomView : UIView

//如果子类是UIButton 在SB中子类无法使用该类

@property(nonatomic,assign)IBInspectable NSInteger widthboard;

-(void)setWidthboard:(NSInteger)widthboard;

@end

#import "CustomView.h"

@implementation CustomView

-(void)setWidthboard:(NSInteger)widthboard{

self.layer.borderWidth = widthboard;

self.layer.borderColor = [UIColor redColor].CGColor;

}

上一篇下一篇

猜你喜欢

热点阅读