ios 實用小技巧

1.控件的圓角問題

CGRect rect = CGRectMake(0, 0, 100, 50);

CGSize radio = CGSizeMake(5, 5);//圓角尺寸

UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//這只圓角位置

UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];

CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];//創(chuàng)建shapelayer

masklayer.frame = button.bounds;

masklayer.path = path.CGPath;//設置路徑

button.layer.mask = masklayer;

舉例為button,其它繼承自UIView的控件都可以

2、navigationBar的透明問題

如果僅僅把navigationBar的alpha設為0的話,那就相當于把navigationBar給隱藏了,大家都知道,父視圖的alpha設置為0的話,那么子視圖全都會透明的。那么相應的navigationBar的標題和左右兩個按鈕都會消失。這樣顯然達不到我們要求的效果。

(1)如果僅僅是想要navigationBar透明,按鈕和標題都在可以使用以下方法:

[self.navigationController.navigationBar setBackgroundImage:[UIImage new]

forBarMetrics:UIBarMetricsDefault];//給navigationBar設置一個空的背景圖片即可實現透明,而且標題按鈕都在

細心的你會發(fā)現上面有一條線如下圖:

這就需要我們做進一步處理,把線去掉,如下方法即可:

self.navigationController.navigationBar.shadowImage = [UIImage new];

//其實這個線也是image控制的。設為空即可

(2)如果你想在透明的基礎上實現根據下拉距離,由透明變得不透明的效果,那么上面那個就顯得力不從心了,這就需要我們采用另外一種方法了

//navigationBar是一個復合視圖,它是有許多個控件組成的,那么我們就可以從他的內部入手

[[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;//這里可以根據scrollView的偏移量來設置alpha就實現了漸變透明的效果


3、全局設置navigationBar標題的樣式和barItem的標題樣式

//UIColorWithHexRGB( )這個方法是自己定義的,這里只需要給個顏色就好了

[[UINavigationBar appearance] setBarTintColor:UIColorWithHexRGB(0xfefefe)];

[[UINavigationBar appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:UIColorWithHexRGB(0xfe6d27)}];

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName : UIColorWithHexRGB(0x666666)} forState:UIControlStateNormal];

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSiz

4、navigationBar隱藏顯示的過度

一個頁面隱藏navigationBar,另一個不隱藏。兩個頁面進行push和pop的時候,尤其是有側滑手勢返回的時候,不做處理就會造成滑動返回時,navigationBar位置是空的,直接顯示一個黑色或者顯示下面一層視圖,很難看。這就需要我們加入過度動畫來隱藏或顯示navigationBar:

在返回后將要出現的頁面實現viewWillAppear方法,需要隱藏就設為YES,需要顯示就設為NO

- (void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[self.navigationController setNavigationBarHidden:NO animated:YES];

}


5、給webView添加頭視圖

webView是一個復合視圖,里面包含有一個scrollView,scrollView里面是一個UIWebBrowserView(負責顯示WebView的內容)

UIView *webBrowserView = self.webView.scrollView.subviews[0];//拿到webView的webBrowserView

self.backHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth*2/3.0)];

[_backHeadImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"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的高度,使其可見


6、模態(tài)跳轉的動畫設置

設置模態(tài)跳轉的動畫,系統提供了四種可供選擇

DetailViewController *detailVC = [[DetailViewController alloc]init];

//UIModalTransitionStyleFlipHorizontal 翻轉

//UIModalTransitionStyleCoverVertical 底部滑出

//UIModalTransitionStyleCrossDissolve 漸顯

//UIModalTransitionStylePartialCurl 翻頁

detailVC.modalTransitionStyle = UIModalTransitionStylePartialCurl;

[self presentViewController:detailVC animated:YES completion:nil];


7、圖片處理只拿到圖片的一部分

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];


8、給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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容