iOS开发技巧

2018-08-03  本文已影响37人  hello_JSH

1. 退去键盘的几种方式

//Objective-C
 [view endEditing:YES]; 
//Swift:
view.endEditing(true)
//Objective-C
[[[UIApplication shardApplication] keyWindow] endEditing: YES];
//Swift
UIApplication.shared.keyWindow?.endEditing(true)
//Objective-C
[[UIApplication shardApplication] sendAction:@selector(resignFirstResponder) to: nil from: nil forEvent: nil];
//Swift
UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)
//Objective-C
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag
//Swift
scrollView.keyboardDismissMode = .onDrag

2.Swift中获取命名空间的名称

let nameSpace = Bundle.init(for: type(of: self)).infoDictionary!["CFBundleExecutable"] as! String
let className = nameSpace + "." + clsName
let classObj = NSClassFromString(className) as! UIViewController.Type
let subVc = classObj.init()

3.设置UITextField/UITextView的'光标颜色'

以UITextField举例,UITextView同理

//Objective-C
{
  //全局修改,影响所用的UITextField
  [[UITextField appearance] setTintColor:[UIColor blackColor]];
  //修改当前UITextField实例对象
  textField.tintColor = [UIColor redColor];
}
//Swift
{
  UITextField.appearance().tintColor = .red;//全局修改
  textField.tintColor = .red;//修改当前
}

4.设置superView的alpha导致subview显示有问题的解决办法

//方案一
{
  superView.layer.shouldRasterize = YES;
  superView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
}
//方案二
{
  superView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
}

5.限制UITextView输入文字的长度。

- (void)textViewDidChange:(UITextView *)textView
{  
    if (textView.markedTextRange == nil && [textView.text length] > 60){ 
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 60)]; 
        [textView.undoManager removeAllActions]; 
        [textView becomeFirstResponder]; 
        return; 
    }
}

6.swift中,在协议中使用associatedtype注意

在协议中一旦加入associatedtype或Self的约束,则该协议只能被用为‘泛型约束’,不能再作为‘独立类型‘的占位使用,也失去了动态派发的特性。

/// 食物协议
protocol Food {}
/// 肉类
class Meat: Food {}
/// 草类
class Grass: Food {}

/// 动物协议
protocol Animal {}
/// 狮子类
class Lion: Animal {}
/// 牛类
class Cow: Animal {}

/// 是狮子吗?
func isLion(_ obj: Animal) -> Bool {
    if obj is Lion {
        return true
    } else {
        return false
    }
}
/// 解决狮子吃肉,牛吃草的问题,但是在判断动物种类时,报错:
// Protocol 'Animal' can only be used as a generic constraint because it has Self or associated type requirements. 就是说Animal不能在参数列表中,作为独立类型使用。
/// 食物协议
protocol Food {}

/// 肉类
class Meat: Food {}
/// 草类
class Grass: Food {}

/// 动物协议
protocol Animal {
    associatedtype F: Food
    func eat(_ food: F);
}
/// 狮子类
class Lion: Animal {
    typealias F = Meat
    func eat(_ food: Meat) {}
}
/// 牛类
class Cow: Animal {
    typealias F = Grass
    func eat(_ food: Grass) {}
}

/// 对比之前的,下面编译报错
/// 是狮子吗?
func isLion(_ obj: Animal) -> Bool {//这行编译器报错:Protocol 'Animal' can only be used as a generic constraint because it has Self or associated type requirements. 就是说Animal不能在参数列表中,作为独立类型使用。
    if obj is Lion {
        return true
    } else {
        return false
    }
}

/// 解决方案
/// 是狮子吗?
func isLion<T: Animal>(_ obj: T) -> Bool {
    if obj is Lion {
        return true
    } else {
        return false
    }
}

上一篇下一篇

猜你喜欢

热点阅读