ImageView圆角 1 2 3 4 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 ; appear.tintColor = UIColor .whiteColor(); appear.barTintColor = UIColor (red: 52 /255, green: 146/ 255 , blue: 233 / 255 , alpha: 1.0 ); appear.backgroundColor = UIColor (red: 253 /255, green: 150/ 255 , blue: 13 / 255 , alpha: 1.0 ); 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 设置为 NOStatus bar style 设置为 UIStatusBarStyleLightContent
1 2 3 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 self .navigationController? .navigationBar.barStyle = UIBarStyle .Black ;
1 2 3 4 override func preferredStatusBarStyle () -> UIStatusBarStyle { return UIStatusBarStyle .LightContent ; }
NavigationController 属性设置
1 2 3 4 5 6 7 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 3 self .tabBarController? .tabBar.hidden = true ;
TableView 1 2 3 self .userbookTableView.contentInset= UIEdgeInsetsMake (- 64 , 0 , 0 , 0 );
TableViewCell 1 2 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 3 self .navigationController? .popViewControllerAnimated(true );
1 2 3 4 self .dismissViewControllerAnimated(true , completion: { ()->Void in })
计算tableCell的高度 定义全局变量
1 2 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" ]; let textHeight = cell.pingjiaLabel.sizeThatFits(CGSizeMake (cell.pingjiaLabel.frame.size.width, CGFloat (FLT_MAX ))).height; 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 ; u.rate = 0.48 ; u.pitchMultiplier = 1.0 ; 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 ) { if (gestureRecognizer == tapRecognizer1){ }else if (gestureRecognizer == tapRecognizer2){ }else if (gestureRecognizer == tapRecognizer3){ } }
保留两位小数 1 String (format: "%.2f" , 3.1415926 );
搜索背景黑块 搜索的时候navigationController会逐渐缩小背景的黑色就会显示出来,解决方法就是修改navigationController的view的背景色
1 2 self .navigationController? .navigationBar.translucent = false ;self .navigationController? .view.backgroundColor = UIColor (red: 250 /255, green: 250/ 255 , blue: 250 / 255 , alpha: 1 );