专栏首页码客iOS常用代码段

iOS常用代码段

ImageView圆角

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

我常用的全局设置

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

//文字白色
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
//文字黑色
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

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

属性设置

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

全局设置

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

属性设置

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

TableView

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

TableViewCell

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

Segue传值

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")
    }
}

边缘手势

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

关闭页面

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

计算tableCell的高度

定义全局变量

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

保存计算高度的Cell实例

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

计算高度

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

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实例

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

NSUserDefaults读写

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

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

清空

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

点击空白隐藏输入法

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

View添加点击事件

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){

   }

}

保留两位小数

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

搜索背景黑块

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

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • iOS文件操作

    剑行者
  • iOS各View的属性设置

    剑行者
  • iOS 图片处理 生成文字图片

    剑行者
  • 【每周一库】- Rayon 数据并行计算库

    Rayon 是一个Rust的数据并行计算库。它非常轻巧,可以轻松地将顺序计算转换为并行计算。同时保证不会有数据争用情况出现。

    MikeLoveRust
  • SQL JOIN

    在机房收费系统个人重构的时候,很多的功能都需要根据数据库中的一个表中的信息,去查询另一个表中相匹配的信息,我们用到了视图,但是你有没有注意到下面的SQL语句呢...

    令仔很忙
  • 分类问题中的维度诅咒(下)

    换句话说,如果可用训练数据的数量是固定的,我们继续添加维度的话,则会发生过拟合。另一方面,如果我们不断增加维度,训练数据的数量需要快速增长以保持相同的覆盖,并避...

    哒呵呵
  • Tree - 109. Convert Sorted List to Binary Search Tree

    109. Convert Sorted List to Binary Search Tree

    用户5705150
  • 1028 人口普查 (20 分)

    某城镇进行人口普查,得到了全体居民的生日。现请你写个程序,找出镇上最年长和最年轻的人。

    可爱见见
  • 记一次gitlab添加用户收不到邮件的解决办法

    之前再gitlab服务器上创建账号可以正常收到邮件,最近就收不到,查了gitlab的配置以及postfix服务都没有问题,后来查看了发信25端口,发现该25端口...

    用户1679793

扫码关注云+社区

领取腾讯云代金券