iOS 筆記

1:在XCode6之后使用pch:

屏幕快照 2016-01-14 上午11.37.22.png

修改工程配置文件,將創建的PCH file的路徑添加到TARGERS->Build Settings->Apple LLVM ->Prefix Header 的選項中去,注意Debug和Release兩欄都要添加:


屏幕快照 2016-01-14 上午11.37.47.png

注意使用$(SRCROOT)關鍵字代替項目文件之前的文件索引。這樣文件位置變化就不會造成錯誤啦。
$(SRCROOT)/JYPickViewDemo/JYPickViewDemo-Prefix.pch

2:錯誤:ld: 15 duplicate symbols for architecture x86_64

屏幕快照 2016-01-16 下午3.08.28.png

搜索工程目錄文件并沒有發現重復。然而:

屏幕快照 2016-01-16 下午3.09.38.png

刪去上圖中的重復文件即可。

注意:imageView的userInteractionEnabled會影響到Button的點擊事件

[self.container addSubview:imageView];
[imageView addSubview:Button];

如果想輸出2016-07-07 這樣的格式,在%后面加上02

NSString *birthDay = [NSString stringWithFormat:@"%ld-%02ld-%02ld", self.datePicker.date.year, self.datePicker.date.month, (long)self.self.datePicker.date.day];

對于collectionView和tableView,注意一下屬性

self.edgesForExtendedLayout = UIRectEdgeNone;
self.automaticallyAdjustsScrollViewInsets = NO;

編譯合并靜態庫

lipo -create /Users/jieyuanzhuang/Library/Developer/Xcode/DerivedData/uiforyysdk-ftqeflovhezxvpbkmqwihldszywq/Build/Products/Debug-iphonesimulator/libuiforyysdk.a /Users/jieyuanzhuang/Library/Developer/Xcode/DerivedData/uiforyysdk-ftqeflovhezxvpbkmqwihldszywq/Build/Products/Release-iphoneos/libuiforyysdk.a  -output /Users/jieyuanzhuang/Desktop/libuiforyysdk.a

UITextField的異常情況,如下圖

@implementation UITextField(UITextFieldForbbidenPaste)

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  if (action == @selector(paste:)) {
    return NO;
  }
  return [super canPerformAction:action withSender:sender];
}

是因為上面的代碼造成的。

自定義present的tip

How to present view controller from right to left in iOS using Swift

Objc

CATransition *transition = [[CATransition alloc] init];
transition.duration = 0.5;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.window.layer addAnimation:transition forKey:kCATransition];
[self presentViewController:dashboardWorkout animated:false completion:nil];

Swift 3

let transition = CATransition()
transition.duration = 0.5
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window!.layer.add(transition, forKey: kCATransition)
present(dashboardWorkout, animated: false, completion: nil)

藍色文件夾和黃色文件夾

藍色文件夾(folder)一般作為資源文件夾使用,與黃色文件夾的主要區別是不參與編譯,所以說如果你在這些文件夾下編寫的邏輯代碼是不參與編譯的,其他文件也不能直接引用它們,若引用其中文件需要全路徑。

添加方式:

選擇Create folder references

黃色文件夾(group)是邏輯文件夾,主要是為了邏輯上的分組,如果手動創建(通過New Group選項)group并不會真正創建一個文件夾文件,該文件夾下的文件則會散亂的存放在工程根目錄下。當然我們通常會讓Xcode中的文件樹與實際工程文件中的文件樹保持一致。

選擇Create groups

最后來說明一下Copy items if needed這個選項

勾選后,會自動復制一份相同的文件到你的工程中,引用的是復制后在工程目錄中的位置。若不勾選,文件的引用位置則是文件的原位置(不建議這樣做,如果該文件在工程外被刪除,工程則無法引用,所以還是復制一份到工程中,這樣更利于工程文件的管理)。

關于自定義searchBar

#import "UISearchBar+Add.h"

@interface GRCustomSearchBar ()<UISearchBarDelegate>
@property (strong, nonatomic) UILabel *searchBarPlaceHolderLabel;
@end

@implementation GRCustomSearchBar

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self conficUI];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self conficUI];
    }
    return self;
}

- (void)dealloc{
    self.delegate = nil;
}

- (void)conficUI{
    self.delegate = self;
    self.backgroundImage = [UIImage new];
    self.barTintColor = [UIColor whiteColor];
    
    UITextField *searchField = [self valueForKey:@"searchField"];
    if (searchField) {
        searchField.backgroundColor = [UIColor whiteColor];
        searchField.layer.cornerRadius = 5.0f;
        searchField.layer.borderColor = [UIColor whiteColor].CGColor;
        searchField.layer.borderWidth = 1;
        searchField.layer.masksToBounds = YES;
        //修正光標顏色
        [searchField setTintColor:RGB(51, 51, 51)];
    }
    
    //設置輸入框文字顏色和字體
    [self fm_setTextColor:RGB(51, 51, 51)];
    [self fm_setTextFont:[UIFont systemFontOfSize:16]];
    
    //自定義 PlaceHolder
    UILabel *label = [UILabel new];
    label.frame = CGRectMake(42, 8, 250, 28);
    label.text = @"型號,材料,風格類型";
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = RGB(229, 229, 229);
    self.searchBarPlaceHolderLabel = label;
    [self addSubview:label];
    
    //自定義搜索Icon
    //先設置一個leftView使得searchField光標右移
    UIButton *tmpButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 18, self.width)];
    searchField.leftView = tmpButton;
    [searchField setLeftViewMode:UITextFieldViewModeAlways];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    [searchField addSubview:searchButton];
    
    //Autolayout
    searchButton.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = NSDictionaryOfVariableBindings(searchButton);
    //設置水平方向約束
    [searchField addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[searchButton(21)]" options:NSLayoutFormatAlignAllRight | NSLayoutFormatAlignAllLeft metrics:nil views:views]];
    //設置高度約束
    [searchField addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[searchButton(21)]" options:NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom metrics:nil views:views]];
    //設置垂直方向居中約束
    [searchField addConstraint:[NSLayoutConstraint constraintWithItem:searchButton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:searchField attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
}

#pragma mark - UISearchBarDelegate

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    self.searchBarPlaceHolderLabel.hidden = searchText.length > 0;
}

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    self.searchBarPlaceHolderLabel.hidden = searchBar.text.length > 0;
    return YES;
}

@end

其中UISearchBar+Add.h

#import "UISearchBar+Add.h"

@implementation UISearchBar (Add)

- (void)fm_setTextFont:(UIFont *)font {
    if (IS_IOS9) {
        [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].font = font;
    }else {
        [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:font];
    }
}

- (void)fm_setTextColor:(UIColor *)textColor {
    if (IS_IOS9) {
        [UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].textColor = textColor;
    }else {
        [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:textColor];
    }
}

- (void)fm_setCancelButtonTitle:(NSString *)title {
    if (IS_IOS9) {
        [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitle:title];
    }else {
        [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:title];
    }
}

- (void)fm_setCancelButtonFont:(UIFont *)font {
    NSDictionary *textAttr = @{NSFontAttributeName : font};
    if (IS_IOS9) {
        [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:textAttr forState:UIControlStateNormal];
    }else {
        [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:textAttr forState:UIControlStateNormal];
    }
}

@end

然而遇到一個很無解的問題:

當輸入超過一行的時候,光標向下偏移。

UITextField 的leftView問題,值得記住

UIImageView *arrowDown = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"p-dropdown"]];
arrowDown.frame = CGRectMake(0, 0, 26, 22);
_province.rightViewMode = UITextFieldViewModeAlways;
_province.rightView = arrowDown;
_address.rightViewMode = UITextFieldViewModeAlways;
_address.rightView = arrowDown;

上面的這種寫法會導致app 卡死,因為兩個不同的UITextField不能使用相同的leftView或rightView.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,546評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,570評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,505評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,017評論 1 313
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,786評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,219評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,287評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,438評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,971評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,796評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,995評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,540評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,230評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,918評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,697評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內容