iOS組件化 01 - 本地私有庫的使用

使用 CocoaPods 創(chuàng)建模板庫

1. 使用終端慎皱,創(chuàng)建模板(pod lib create 模板名)

pod lib create SLBaseKit
pod lib create SLBaseKit

2. 刪除下圖中紅框的文件

remove files

3. 修改.gitignore文件地来,添加上面刪除的文件

# .gitignore 

**/Pods/
**/*.xcworkspace

4. 提交本地倉庫代碼

git add .
git commit -m "create SLBaseKit project template & modify .gitignore"

5. 添加組件代碼

(1) 拖入開發(fā)好的組件代碼文件
(2) 刪除 ReplaceMe.m 文件

SLBaseKit/SLBaseKit/Classes/

組件相關(guān)代碼

//  SLSearchBar.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SLSearchBar : UITextField

/**
 *  添加一個搜索框
 *
 *  @return 搜索框
 */
+ (instancetype)searchBar;

@end

NS_ASSUME_NONNULL_END
//  SLSearchBar.m

#import "SLSearchBar.h"

#define SLSearchBar_BundleName @"SLBaseKit"
@implementation SLSearchBar

+ (NSBundle *)currentBundle {
    static NSBundle *bundle;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:self]
                                           pathForResource:SLSearchBar_BundleName
                                           ofType:@".bundle"]];
        if (!bundle) bundle = [NSBundle mainBundle];
    });
    return bundle;
}

+ (instancetype)searchBar {
    SLSearchBar *searchBar = [[SLSearchBar alloc] init];
    searchBar.bounds = CGRectMake(0, 0, 300, 30);
    searchBar.font = [UIFont systemFontOfSize:13.0];
    searchBar.placeholder = @"請輸入搜索內(nèi)容條件";

    NSString *searchBarImagePath = [[self currendBundle] pathForResource:@"searchbar_textfield_background@2x" ofType:@"png"];
    UIImage *searchBarImage = [UIImage imageWithContentsOfFile:searchBarImagePath];
    
    CGFloat searchBarW = searchBarImage.size.width * 0.5;
    CGFloat searchBarH = searchBarImage.size.height * 0.5;
    
    // 設(shè)置背景圖片為可拉伸模式的
    searchBar.background = [searchBarImage resizableImageWithCapInsets:UIEdgeInsetsMake(searchBarH, searchBarW, searchBarH, searchBarW) resizingMode:UIImageResizingModeStretch];
    
    UIImageView *searchIcon = [[UIImageView alloc] init];
    searchIcon.bounds = CGRectMake(0, 0, 30, 30);

    NSString *searchIconImagePath = [[self currentBundle] pathForResource:@"searchbar_textfield_search_icon@2x" ofType:@"png"];
    searchIcon.image = [UIImage imageWithContentsOfFile:searchIconImagePath];

    // 內(nèi)容模式居中
    searchIcon.contentMode = UIViewContentModeCenter;
    
    // 左邊的視圖
    searchBar.leftView = searchIcon;
    // 顯示模式
    searchBar.leftViewMode = UITextFieldViewModeAlways;
    
    return searchBar;
}

@end

(3)添加圖片資源(如果你的組件沒有依賴圖片壳贪,可忽略此步驟)


add resource

(4) 打開SLBaseKit.podspec

open SLBaseKit.podspec

(5)修改SLBaseKit.podspec

#
# Be sure to run `pod lib lint SLBaseKit.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'SLBaseKit' # 組件的名稱
  s.version          = '0.1.0' # 版本
  s.summary          = 'SLBaseKit 是一個基礎(chǔ)組件' # 摘要信息

# This description is used to generate tags and improve search results.
#   * Think: What does it do? Why did you write it? What is the focus?
#   * Try to keep it short, snappy and to the point.
#   * Write the description between the DESC delimiters below.
#   * Finally, don't worry about the indent, CocoaPods strips it!
  
  # 描述信息
  s.description      = <<-DESC
  container
  1. SLSearchBar 繼承了UITextField, 可以快速創(chuàng)建一個搜索框 
                       DESC

  s.homepage         = 'https://github.com/CoderSLZeng/SLBaseKit'  # 組件首頁遠程地址
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { '一夜知秋' => '359297567@qq.com' }                                                # 作者聯(lián)系方式
  s.source           = { :git => 'https://github.com/CoderSLZeng/SLBaseKit.git', :tag => s.version.to_s } # 源碼遠程地址
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0' # 最低部署版本

  s.source_files = 'SLBaseKit/Classes/**/*' # 源碼所在路徑
  
  # 如果你的組件沒有使用到圖片資源箫津,可以注釋掉下面3行
  s.resource_bundles = {
    'SLBaseKit' => ['SLBaseKit/Assets/*.png'] # 資源文件路徑
  }

  # s.public_header_files = 'Pod/Classes/**/*.h' # 如果不開源愈污,需要的接口頭文件路徑
  # s.frameworks = 'UIKit', 'MapKit' # 依賴的動態(tài)庫
  # s.dependency 'AFNetworking', '~> 2.3' # 依賴的第三方框架
end

(6)回到剛才的終端碍扔,執(zhí)行下面命令

# 根據(jù)自己的路徑修改
cd SLBaseKit/SLBaseKit/Example
pod install

(7)打開SLBaseKit.xcworkspace回季,觀察 Xcode 導(dǎo)航欄家制,可以看到組件代碼和圖片資源被添加到Development Pods 的相關(guān)文件夾中

Snip20191227_29.png

6. 測試組件

(1)在第1步創(chuàng)建組件模板的時候,已經(jīng)默認創(chuàng)建好了一個Example for SLBaseKit 的測試工程泡一,

Example for SLBaseKit

(2)同時觀察Podfile文件颤殴,已經(jīng)默認引用了SLBaseKit的本地庫

Podfile

pod 'SLBaseKit', :path => '../' 默認是使用的本地庫,后面還會講解 遠程庫的使用
path => '../'表示到上一層目錄的路徑

目錄層級

后期可根據(jù)的宿主工程所在路徑自定義修改鼻忠,例如:


組件化目錄
# 編輯Podfile修改下面代碼
 pod 'SLBaseKit', :path => '../../本地庫/基礎(chǔ)組件/基礎(chǔ)/SLBaseKit/'

(2)修改 SLViewController.m

SLViewController.m

(2)編譯測試工程涵但,運行顯示 UI 效果

SLViewController UI

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市帖蔓,隨后出現(xiàn)的幾起案子矮瘟,更是在濱河造成了極大的恐慌,老刑警劉巖塑娇,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件澈侠,死亡現(xiàn)場離奇詭異,居然都是意外死亡埋酬,警方通過查閱死者的電腦和手機哨啃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門烧栋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拳球,你說我怎么就攤上這事审姓。” “怎么了祝峻?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵邑跪,是天一觀的道長。 經(jīng)常有香客問我呼猪,道長画畅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任宋距,我火速辦了婚禮轴踱,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘谚赎。我一直安慰自己淫僻,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布壶唤。 她就那樣靜靜地躺著雳灵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪闸盔。 梳的紋絲不亂的頭發(fā)上悯辙,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天,我揣著相機與錄音迎吵,去河邊找鬼躲撰。 笑死,一個胖子當(dāng)著我的面吹牛击费,可吹牛的內(nèi)容都是我干的拢蛋。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蔫巩,長吁一口氣:“原來是場噩夢啊……” “哼谆棱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起圆仔,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤垃瞧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后荧缘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體皆警,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年截粗,在試婚紗的時候發(fā)現(xiàn)自己被綠了信姓。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡绸罗,死狀恐怖意推,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情珊蟀,我是刑警寧澤菊值,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站育灸,受9級特大地震影響腻窒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜磅崭,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一儿子、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧砸喻,春花似錦柔逼、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至癣漆,卻和暖如春维咸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背惠爽。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工腰湾, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人疆股。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓费坊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親旬痹。 傳聞我的和親對象是個殘疾皇子附井,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容