IOS手札

本文汇总了iOS开发中常用的技巧,包括不同设备屏幕尺寸适配、自定义提示语颜色设置、图片点击事件处理、视图点击拦截等,还介绍了如何实现毛玻璃效果、添加边框、设置圆角以及处理UITableView分割线等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

iphone尺寸
iphone5:320*568
iphone6:375*667
iphone6 plus:414*736/1.15

NSLog打印的字符串编码为: Unicode


自定义提示语颜色 
var placeholder = NSAttributedString(string: " 请输入短信验证码 ", attributes: [NSForegroundColorAttributeName : UIColor(red: 68/255, green: 68/255, blue: 68/255, alpha: 1)])
self.noteTextField?.attributedPlaceholder=placeholder


图片点击事件
UIImage *image = [UIImage imageNamed:imageName]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    imageView.frame = self.view.bounds; 
    imageView.userInteractionEnabled = YES; 
 
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissGuideView)]; 
    [imageView addGestureRecognizer:tap]; 
 
    [self.view addSubview:imageView]; 

视图截获所有子视图的点击事件,使子视图不能获取点击
  override func hitTest(point: CGPoint , withEvent event: UIEvent ?) -> UIView ? {
        return self//返回本视图,不再向下传递
    }

获取版本号
[[[NSBundle mainBundle] infoDictionary] objectForKey:@ "CFBundleShortVersionString" ]

引导页判断
[[ NSUserDefaults standardUserDefaults ] setObject : @"show" forKey :[ @"showGuidence" stringByAppendingString : [[[NSBundle mainBundle] infoDictionary] objectForKey:@ "CFBundleShortVersionString" ] ]];//需要和版本号关联
[[ NSUserDefaults standardUserDefaults ] synchronize ];

毛玻璃效果

UIVisualEffectView *visualEffect = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];

    visualEffect.frame = CGRectMake(2090280300);

    visualEffect.alpha = 0.9;

    [ self. view  addSubview:visualEffect];

UIView单面边框
 myImageView. layer . borderWidth = 0.0
               
let topBorder= CALayer ()
                topBorder.
frame = CGRectMake ( 0 , 0 , myImageView. frame . width , 1 )
                topBorder.
backgroundColor = UIColor . blackColor (). CGColor
                myImageView.
layer . addSublayer (topBorder)
               
               
let leftBorder= CALayer ()
                leftBorder.
frame = CGRectMake ( 0 , 0 , 1 , myImageView. frame . height )
                leftBorder.
backgroundColor = UIColor . blackColor (). CGColor
                myImageView.
layer . addSublayer (leftBorder)
               
               
let bottomBorder= CALayer ()
                bottomBorder.
frame = CGRectMake ( 0 , myImageView. frame . height , myImageView. frame . width , 1 )
                bottomBorder.
backgroundColor = UIColor . blackColor (). CGColor
                myImageView.layer.addSublayer(bottomBorder)

分割线
  let topBorder=CALayer()
                topBorder.frame=CGRectMake(0, 0, myImageView.frame.width, 1)
                topBorder.backgroundColor=UIColor.blackColor().CGColor
                myImageView.layer.addSublayer(topBorder)

                let bottomBorder=CALayer()
                bottomBorder.frame=CGRectMake(0, myImageView.frame.height, myImageView.frame.width, 1)
                bottomBorder.backgroundColor=UIColor.blackColor().CGColor
                myImageView.layer.addSublayer(bottomBorder)

UIView *hView = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , ScreenWidth , 30 )];
   
CALayer *bottomBorder=[[ CALayer alloc ] init ];
    bottomBorder.
frame = CGRectMake ( 0 , 0 , hView. frame . size . width , 0.5 );
    bottomBorder.
backgroundColor = lineGrayColor . CGColor ;
    [hView.layer addSublayer:bottomBorder ];


//01,02...09,10,11...
%02d

DEBUG和Release修改
然后点击Edit Scheme后进行修改

在本视图控制器下弹出另一个视图控制器的内容
[ self . navigationController . view addSubview :skuController. view ];

UILabel不同的字体颜色
  NSMutableAttributedString *orderStateAttributedString = [[ NSMutableAttributedString alloc ] initWithString : @" 订单状态:暂无 " ];
    [orderStateAttributedString
addAttribute : NSForegroundColorAttributeName value :[ UIColor blackColor ] range : NSMakeRange ( 0 , 5 )];
    [orderStateAttributedString
addAttribute : NSFontAttributeName value :[ UIFont systemFontOfSize : 14 ] range : NSMakeRange ( 0 , 5 )];
    self.orderStateLabel.attributedText = orderStateAttributedString;

UIView点击事件

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Actiondo:)];
    [uiview addGestureRecognizer:tapGesture];
-(void)Actiondo: ( UITapGestureRecognizer *)recognizer{
//获取所点击视图recognizer. view
}

自动布局动画和重绘
 [ self setNeedsLayout ];
    [self layoutIfNeeded];
-setNeedsUpdateConstraints和-layoutIfNeeded两个方法来刷新约束的改变,使UIView重新布局。
 [ self . contentView setNeedsLayout ];
        [
self . contentView layoutIfNeeded ];
        [
self . contentView removeConstraint : self . photographButtonConstraint ];
       
self . photographButtonConstraint = [ NSLayoutConstraint
                                          
constraintWithItem : self . photographButton
                                          
attribute : NSLayoutAttributeLeft
                                          
relatedBy : NSLayoutRelationEqual
                                          
toItem : self . photographImageViewArray [imageArray. count ]
                                          
attribute : NSLayoutAttributeLeft
                                          
multiplier : 1
                                          
constant : 0 ];
        [self.contentView addConstraint:self.photographButtonConstraint];


   self.addressContentText.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    self.addressContentText.leftViewMode = UITextFieldViewModeAlways;


frameView.layer.borderWidth = 1;
        frameView.layer.borderColor = [[UIColor whiteColor] CGColor];

圆角
       //设置圆角边框
        myview.layer.cornerRadius = 5;
        myview.layer.masksToBounds = YES;
        //设置边框及边框颜色
        myview.layer.borderWidth = 0.5;
        myview.layer.borderColor =[ [UIColor grayColor] CGColor];

局部圆角
  UIBezierPath *maskPath = [ UIBezierPath bezierPathWithRoundedRect : self . submitAlertView . bounds byRoundingCorners : UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii : CGSizeMake ( 10 , 10 )];
   
CAShapeLayer *maskLayer = [[ CAShapeLayer alloc ] init ];
    maskLayer.
frame = self . submitAlertView . bounds ;
    maskLayer.
path = maskPath. CGPath ;
    self.submitAlertView.layer.mask = maskLayer;

UITableView 去除底部多余分割线
 [ tableView setTableFooterView :[[ UIView alloc ] init ]];

UILabel多行
  self . promptContent . lineBreakMode = NSLineBreakByWordWrapping ;
    self.promptContent.numberOfLines = 0;//上面两行设置多行显示
//单行时顶部对齐
self . text  = [ self . text stringByAppendingString :@ "\n " ];

快速创建字典
[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt: self .page], @"page" ,[NSNumber numberWithInt: 10 ], @"limit" , self .ids, @"status_id" , nil ]

通知其他类调用方法

  其他类中先写入:[[ NSNotificationCenter defaultCenter ] addObserver : self
                                           selector:@selector( mytest: )
                                               name : @"reloadMessageData"
                                             object:nil];

- ( void ) mytest:(NSNotification*) notification
{
    id obj = [notification object]; //获取到传递的对象

本类调用:[[ NSNotificationCenter defaultCenter ]
     
postNotificationName : @"reloadMessageData"
                    object : @“ 传递的对象 " ];

跳转时退出当前页面:
        NSMutableArray *navigationArray = [[ NSMutableArray alloc ] initWithArray : self . navigationController . viewControllers ];
        [navigationArray
removeObjectAtIndex : navigationArray. count - 1 ];  // You can pass your index here
        self.navigationController.viewControllers = navigationArray;

快捷加入动画效果
[ UIView animateWithDuration : 0.5 animations :^
     {
        
CGRect tempFrame = self . infoView . frame ;
         tempFrame.
origin . y += 40 ;
        
self . infoView . frame = tempFrame;
        
        
CGRect scrollFrame = self . myScrollView . frame ;
         scrollFrame.
origin . y += 40 ;
        
self . myScrollView . frame = scrollFrame;
     }
completion :^( BOOL finished){
     }];

 //清空已有控件
   
NSArray *gardenOptionsArray = self . gardenOptionsView . subviews ;
   
for ( int i = 0 ;i<gardenOptionsArray. count ;i++){
       
UIView *clearView = ( UIView *)gardenOptionsArray[i];
        [clearView
removeFromSuperview ];
    }


//按钮图片位置
[ leftButton setImage :[ UIImage imageNamed : @"hh_ico_badge" ] forState : UIControlStateNormal ];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -0, 0, 10);

UIView的显示位置:


将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront()方法。

将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack()方法。

[self.view insertSubview:girlView belowSubview:bottomView];//把girlView插入到bottomView后面

 [self.view insertSubview:girlView aboveSubview:bottomView];//把girlView插入到bottomView前面


 [ self .view insertSubview : girlView atIndex:0 ];//把girlView插入到0层

    



alpha 属性为0.0时视图完全透明,为1.0时视图完全不透明。

    hidden属性为YES时视图隐藏,否则不隐藏。

    这两种方式的效果是相同的。

    视图的alpha值会影响子视图的绘制,但是子视图的alpha值不变。

    子视图真正渲染出来的alpha值等于子视图的alpha乘父视图的alpha。


AppStore检查更新的json地址http://itunes.apple.com/lookup?id=%@&country=cn

  1. // 如果要实现在应用里面跳到appstore的对应评论页面里面的话,只要将下面地址中App_ID替换成自己的id就可以了,其他的地方都不用管。  
  2. // 如果要用Safari浏览器做实验的话可以将地址中的 "itms-apps://" 替换成"http://"即可。  
  3. // 另外也可以尝试地改变其中几个参数的数值,可以看看结果。  
  4.  // 注意: 必须使用真机调试,目前来说还是不支持模拟器运行的  
  5.   
  6.   
  7.   
  8. NSString * appstoreUrlString = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?mt=8&onlyLatestVersion=true&pageNumber=0&sortOrdering=1&type=Purple+Software&id=APP_ID";  
  9.       
  10.     NSURL * url = [NSURL URLWithString:appstoreUrlString];  
  11.       
  12.     if ([[UIApplication sharedApplication] canOpenURL:url])  
  13.     {  
  14.         [[UIApplication sharedApplication] openURL:url];  
  15.     }  
  16.     else  
  17.     {  
  18.         NSLog(@"can not open");  
  19.     }  

检测ios系统版本:
[[[UIDevice currentDevice] systemVersion] floatValue]

企业安装包上传网址:fir.im

查看静态库信息,在终端下输入: lipo -info libUPPayPlugin.a


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值