代码笔记

Marsonsy约束细节

1、优先设置label大小,高度固定,水平自适应

// 水平方向别扯我
[label1 setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];

NavigationBar的透明问题

如果仅仅把navigationBar的alpha设为0的话,那就相当于把navigationBar给隐藏了,大家都知道,父视图的alpha设置为0的话,那么子视图全都会透明的。那么相应的navigationBar的标题和左右两个按钮都会消失。这样显然达不到我们要求的效果。
一、如果仅仅是想要navigationBar透明,按钮和标题都在可以使用以下方法:
//给navigationBar设置一个空的背景图片即可实现透明,而且标题按钮都在
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

如果发现有一条黑线啊(如图)


代码笔记_第1张图片
20170307153959_956.jpg

可以用以下方法去除

//其实这个线也是image控制的。设为空即可 
self.navigationController.navigationBar.shadowImage = [UIImage new];  
二、如果你想在透明的基础上实现根据下拉距离,由透明变得不透明的效果,那么上面那个就显得力不从心了,这就需要我们采用另外一种方法了
//navigationBar是一个复合视图,它是有许多个控件组成的,那么我们就可以从他的内部入手 ,这里可以根据scrollView的偏移量来设置alpha就实现了渐变透明的效果 
[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0; 
三、navigationBar隐藏显示的过度
相信在使用中肯定遇到过,一个页面隐藏navigationBar,另一个不隐藏。两个页面进行push和pop的时候,尤其是有侧滑手势返回的时候,不做处理就会造成滑动返回时,navigationBar位置是空的,直接显示一个黑色或者显示下面一层视图,很难看。这就需要我们加入过度动画来隐藏或显示navigationBar:

在返回后将要出现的页面实现viewWillAppear方法,需要隐藏就设为YES,需要显示就设为NO

- (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [self.navigationController setNavigationBarHidden:NO animated:YES]; 
}  

给webView添加头视图

webView是一个复合视图,里面包含有一个scrollView,scrollView里面是一个UIWebBrowserView(负责显示WebView的内容)
   //拿到webView的webBrowserView 
   UIView *webBrowserView = self.webView.scrollView.subviews[0];    self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenWidth*2/3.0)]; 
    [_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImageimageNamed:@"placeholderImage"]]; 
    [self.webView insertSubview:_backHeadImageView belowSubview:self.webView.scrollView];  
   //把backHeadImageView插入到webView的scrollView下面 
    CGRect frame = self.webBrowserView.frame; 
    frame.origin.y = CGRectGetMaxY(_backHeadImageView.frame); 
    self.webBrowserView.frame = frame; 
    //更改webBrowserView的frame向下移backHeadImageView的高度,使其可见  

图片处理只拿到图片的一部分

UIImage *image = [UIImage imageNamed:filename]; 
CGImageRef imageRef = image.CGImage; 
CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height); 
//这里的宽高是相对于图片的真实大小 
 
//比如你的图片是400x400的那么(0,0,400,400)就是图片的全尺寸,想取哪一部分就设置相应坐标即可 
CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect); 
UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];  

给UIView设置图片

UIImage *image = [UIImage imageNamed:@"playing"]; 
_layerView.layer.contents = (__bridge id)image.CGImage; 
_layerView.layer.contentsCenter = CGRectMake(0.25, 0.25, 0.5, 0.5); 
//同样可以设置显示的图片范围 
//不过此处略有不同,这里的四个值均为0-1之间;对应的依然是写x,y,widt,height  

你可能感兴趣的:(代码笔记)