使用 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