美文网首页
iOS 笔记

iOS 笔记

作者: Saxon_Geoffrey | 来源:发表于2016-01-14 11:46 被阅读145次

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.

相关文章

网友评论

      本文标题:iOS 笔记

      本文链接:https://www.haomeiwen.com/subject/xuzckttx.html