UITableView
TableView中取消選中顏色變化,在didSelectRowAtIndexPath中寫入[tableView deselectRowAtIndePath]就可以取消選中狀態(tài)。當然我們可以用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];
設置半透明效果,若是半透明的那么顏色,透明度都會有系統(tǒng)偏移,有半透明效果。建議設置為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條順序更改導航欄的左側按鈕(本段摘自網(wǎng)絡):
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];
或者
//所有的子界面返回時都變成了我們定義的文字,如果不想顯示文字,直接"",就會單獨顯示一個系統(tǒng)的返回箭頭圖標,也是很清晰的感覺。
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值。那么建議隱藏系統(tǒng)的返回按鈕自定義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;
}