由于項(xiàng)目需要最近多navigation的操作頗多趴生,為了便于以后使用也方便大家猪半,所以在這里一一記錄下來(lái)(以375*667為例)
</br>
1.設(shè)置navigationBar透明(系統(tǒng)默認(rèn))
代碼:
[self.navigationController.navigationBar setTranslucent:YES];
效果:可以使得navigationBar呈現(xiàn)半透明搀突,類似UIVisualEffectView的效果,并且可以增加tableView的滑動(dòng)區(qū)域(注意是滑動(dòng)區(qū)域而不是滾動(dòng)區(qū)域)。意思是
可以設(shè)置
self.tableView= [[UITableViewalloc]initWithFrame:self.view.bounds];
實(shí)際效果為tableview的實(shí)際顯示區(qū)域只有 667 - 64;但是他的有效滾動(dòng)區(qū)域卻是667
/*
New behavior on iOS 7.
Default is YES.
You may force an opaque background by setting the property to NO.
If the navigation bar has a custom background image, the default is inferred
from the alpha values of the image—YES if it has any pixel with alpha < 1.0
If you send setTranslucent:YES to a bar with an opaque custom background image
it will apply a system opacity less than 1.0 to the image.
If you send setTranslucent:NO to a bar with a translucent custom background image
it will provide an opaque background for the image using the bar's barTintColor if defined, or black
for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.
>*/
根據(jù)上面說(shuō)法,當(dāng)設(shè)置背景圖的時(shí)候闻妓,只要背景圖的alpha<1 ,那么translucent就會(huì)被設(shè)置為YES掠械。
</br>
2.關(guān)于automaticallyAdjustsScrollViewInsets
代碼:
self.automaticallyAdjustsScrollViewInsets=YES;
效果:在有navigationBar的狀態(tài)下由缆,scrollView會(huì)默認(rèn)被下推64px,就是因?yàn)槟J(rèn)設(shè)置self.automaticallyAdjustsScrollViewInsets=YES 。 這樣的狀態(tài)在navigationBar是透明的情況下是可以設(shè)置為NO并且有效的猾蒂,但是在translucent = NO的情況下不能改變均唉。
</br>
3.設(shè)置navigationBar全透明
1.用rgbColor創(chuàng)建一張透明的圖片設(shè)置為背景圖
<pre><code>
[self.navigationController.navigationBarsetBackgroundImage:[UIImagecreateImageWithColor:[UIColorclearColor]]forBarMetrics:UIBarMetricsDefault];
</code></pre>
2.找到并設(shè)置navigationBar的imageView線隱藏
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
return (UIImageView *)view;
}
for (UIView *subview in view.subviews) {
UIImageView *imageView = [self findHairlineImageViewUnder:subview];
if (imageView) {
return imageView;
}
}
return nil;
}
</br>
4.滾動(dòng)tableView、scrollView婚夫,讓navigationbar的隱藏的2種方法
方法一
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
if (scrollView == self.containerView) {
if(velocity.y>0)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
else
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
}
方法二
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//<0的話是正常
if (_tableView.contentOffset.y <= -64) {//顯示
self.navigationController.navigationBar.frame = CGRectMake(0, 20, 375, 44);
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
else if (_tableView.contentOffset.y < 0){
self.navigationController.navigationBar.frame = CGRectMake(0, 20 - (_tableView.contentOffset.y + 64) , 375, 44);
}else{
self.navigationController.navigationBar.frame = CGRectMake(0, -44, 375, 44);
[self.navigationController setNavigationBarHidden:YES animated:NO];
}
//-64 是正常的contentoff
}
</br>
5.navigationbar的顏色漸變
顏色漸變也可以參考navigationbar隱藏的方案二方法浸卦,實(shí)現(xiàn)起來(lái)還是比較方便的
</br>