iOS常用代码段

ImageView圆角

1
2
3
var layer = cell.leftImageView.layer;
layer.masksToBounds=true;
layer.cornerRadius = cell.leftImageView.bounds.size.width/2;

我常用的全局设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
let appear = UINavigationBar.appearance();
appear.translucent = false;
//设置Item的样式
appear.tintColor = UIColor.whiteColor();
//设置bar的颜色
appear.barTintColor = UIColor(red: 52/255, green: 146/255, blue: 233/255, alpha: 1.0);
//设置背景色(不透明时没用,因为barTintColor在backgroundColor的上一层)
appear.backgroundColor = UIColor(red: 253/255, green: 150/255, blue: 13/255, alpha: 1.0);
//去掉navigationBar下的黑线
appear.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
appear.shadowImage = UIImage();
//设置标题样式
appear.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Heiti SC", size: 18.0)!];

let tabbarAppear = UITabBar.appearance();
tabbarAppear.tintColor = UIColor(red: 56/255, green: 173/255, blue: 255/255, alpha: 1.0);

let searchBarAppear = UISearchBar.appearance();
searchBarAppear.translucent = false;
searchBarAppear.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1.0);
searchBarAppear.barTintColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1.0);
searchBarAppear.layer.borderColor = UIColor(hexString: "#ffffff")!.CGColor;
searchBarAppear.layer.borderWidth = 0;
searchBarAppear.backgroundImage = UIImage();

注意优先级顺序

控制器中代码设置 > storybord设置 > 全局设置
优先级高的会覆盖优先级低的配置,比如storybord中的设置了navigationbar的样式 那么全局设置就不生效

设置状态栏

iOS9以下

Info.plist添加两个配置项
View controller-based status bar appearance 设置为 NO
Status bar style 设置为 UIStatusBarStyleLightContent

1
2
//文字白色
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
1
2
//文字黑色
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)

iOS7-9

ios升到9以后上面的设置会报一下错误
CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace

Info.plist添加两个配置项
View controller-based status bar appearance 设置为 YES

1
2
3
//navigationController管理的页面
//这样是设置是为了让状态栏文字变成白色
self.navigationController?.navigationBar.barStyle = UIBarStyle.Black;
1
2
3
4
//无navigationController的页面
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.LightContent;
}

属性设置

1
2
3
4
5
6
//设置是否透明
self.navigationController?.navigationBar.translucent = false;
//是否隐藏
self.navigationController?.navigationBarHidden = true;
//设置标题
self.navigationItem.title = "我是标题";

全局设置

1
2
3
4
var appear = UINavigationBar.appearance();
appear.tintColor = UIColor.orangeColor();
appear.translucent = true;
appear.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor(),NSFontAttributeName: UIFont(name: "Heiti SC", size: 18.0)!];

TabBarController

属性设置

1
2
//是否隐藏
self.tabBarController?.tabBar.hidden = true;

TableView

1
2
//设置偏移
self.userbookTableView.contentInset=UIEdgeInsetsMake(-64, 0, 0, 0);

TableViewCell

1
2
//设置cell右面的图标
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator;

Segue传值

1
2
3
4
5
6
7
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var dv = segue.destinationViewController as! UIViewController;
var isExist = dv.respondsToSelector(Selector("naviTitle"));
if(isExist){
dv.setValue(selectCellName, forKey: "naviTitle")
}
}

边缘手势

1
2
//禁止边缘手势
self.navigationController?.interactivePopGestureRecognizer!.enabled = false;

关闭页面

1
2
//关闭push的页面
self.navigationController?.popViewControllerAnimated(true);
1
2
3
4
//关闭model的页面
self.dismissViewControllerAnimated(true, completion: {
()->Void in
})

计算tableCell的高度

定义全局变量

1
2
//用于缓存计算高度的cell
var offscreenCells:[String:AnyObject] = [:];

保存计算高度的Cell实例

1
2
let cell = NSBundle.mainBundle().loadNibNamed("PingjiaTableViewCell", owner: nil, options: nil)[0] as! PingjiaTableViewCell;
self.offscreenCells["PingjiaTableViewCell"] = cell;

计算高度

1
2
3
4
5
6
7
8
9
10
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let item = tableItem[indexPath.section][indexPath.row];
let cell = self.offscreenCells["PingjiaTableViewCell"] as! PingjiaTableViewCell;
cell.pingjiaLabel.text = item["text"];
//不定高度的label的高度
let textHeight = cell.pingjiaLabel.sizeThatFits(CGSizeMake(cell.pingjiaLabel.frame.size.width, CGFloat(FLT_MAX))).height;
//把label当成一行所得到的高度
let minHeight = cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height + 1;
return textHeight + minHeight - 10;
}

TTS

1
2
3
4
5
6
7
let player = AVSpeechSynthesizer();
let u = AVSpeechUtterance(string: "今天天气不错挺风和日丽的!");
u.voice = AVSpeechSynthesisVoice(language: "zh-CN");
u.volume = 1.0;//音量 [0-1] Default = 1
u.rate = 0.48;//播放速度
u.pitchMultiplier = 1.0;//播放基准音调 [0.5 - 2] Default = 1
player.speakUtterance(u);

获取AppDelegate实例

1
let appDelegate=UIApplication.sharedApplication().delegate as! AppDelegate;

NSUserDefaults读写

1
2
3
let defaults = NSUserDefaults.standardUserDefaults();
defaults.setObject("zhangjian", forKey: "imUserName");
defaults.synchronize();

1
2
let defaults = NSUserDefaults.standardUserDefaults();
let userName = defaults.stringForKey("imUserName");

清空

1
2
3
let defaults = NSUserDefaults.standardUserDefaults();
let domainName = NSBundle.mainBundle().bundleIdentifier!;
defaults.removePersistentDomainForName(domainName);

点击空白隐藏输入法

1
2
3
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true);
}

View添加点击事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var tapRecognizer1:UITapGestureRecognizer!;
var tapRecognizer2:UITapGestureRecognizer!;
var tapRecognizer3:UITapGestureRecognizer!;

//初始化
tapRecognizer1 = UITapGestureRecognizer(target: self, action: "presentImagePicker:");
tapRecognizer2 = UITapGestureRecognizer(target: self, action: "presentImagePicker:");
tapRecognizer3 = UITapGestureRecognizer(target: self, action: "presentImagePicker:");

//添加事件
card1Image.addGestureRecognizer(tapRecognizer1);
//别忘了加这句
card1Image.userInteractionEnabled = true;
card2Image.addGestureRecognizer(tapRecognizer2);
card2Image.userInteractionEnabled = true;
card3Image.addGestureRecognizer(tapRecognizer3);
card3Image.userInteractionEnabled = true;

func presentImagePicker(gestureRecognizer: UITapGestureRecognizer) {
//触发事件的View
//gestureRecognizer.view
if(gestureRecognizer == tapRecognizer1){

}else if(gestureRecognizer == tapRecognizer2){

}else if(gestureRecognizer == tapRecognizer3){

}

}

保留两位小数

1
String(format: "%.2f", 3.1415926);

搜索背景黑块

搜索的时候navigationController会逐渐缩小背景的黑色就会显示出来,解决方法就是修改navigationControllerview的背景色

1
2
self.navigationController?.navigationBar.translucent = false;
self.navigationController?.view.backgroundColor = UIColor(red: 250/255, green: 250/255, blue: 250/255, alpha: 1);