標籤:
---- 說在前面的話
今天分享兩個東西: 1.UITableView系統原生分割線,怎麼設定頂格?
2.怎麼給UIView設定兩邊是圓角,其它兩邊是直角的效果?
一.UITableVew內建的分割線,左邊有10X的縮排,相信小夥伴們也試過setSeparatorInset這個方法,具體效果自己試吧,反正不管用.如果原型圖上是頂格的分割線,怎麼辦?自訂cell裡畫一條線?當然可以!但是超麻煩,不是嗎?
今天給大家分享一個超簡單的方法.遵守<UITableViewDelegate>,在-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath這個代理方法中做一些設定即可.代碼如下:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; }}
OK 搞定!
二.上一張原型圖,說明一下需求
上邊就是半邊圓角,半邊直角的效果.好了,廢話不多說,思路如下: 給UIView添加個分類,設定一些枚舉值(哪邊要圓角),自訂CAShapeLayer,給UIView的layer添加即可,
代碼如下:
#import <UIKit/UIKit.h>typedef enum { YYSideStyleLeft, // 左上左下圓角 YYSideStyleTop, // 左上右上圓角 YYSideStyleBottom,// 左上右下圓角 YYSideStyleRight // 右上右下圓角 } YYSideStyle;@interface UIView (estension)/** 設定某兩邊圓角效果 -- 需要制定bounds */- (void)roundSide:(YYSideStyle)side;@end#import "UIView+estension.h"@implementation UIView (estension)- (void)roundSide:(YYSideStyle)side{ UIBezierPath *maskPath; if (side == YYSideStyleLeft) maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerBottomLeft) cornerRadii:CGSizeMake(5.f, 5.f)]; else if (side == YYSideStyleRight) maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopRight|UIRectCornerBottomRight) cornerRadii:CGSizeMake(5.f, 5.f)]; else if (side == YYSideStyleTop) maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) cornerRadii:CGSizeMake(5.f, 5.f)]; else maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight) cornerRadii:CGSizeMake(5.f, 5.f)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath; self.layer.mask = maskLayer; [self.layer setMasksToBounds:YES];}
調用:
[loginBtn roundSide:YYSideStyleRight];
注意點:需設定視圖的frame才能顯示
OK 搞定!
iOS開發黑魔法