iOS開發黑魔法

來源:互聯網
上載者:User

標籤:

---- 說在前面的話

    今天分享兩個東西: 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開發黑魔法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.