您的位置:首页 > 移动开发 > IOS开发

iOS 开发中遇到的小问题

2015-12-28 11:12 465 查看
1、修改当前视图的透明度,而不影响子视图的透明度

self.backgroundColor
= [[UIColor
whiteColor]
colorWithAlphaComponent:0];

2、push之前将自己移除掉,使view返回时直接返回到父类

ExamineeSelectSubjectController * examineepersonal = [ExamineeSelectSubjectController
loadFromNib:dic];
[examineepersonal
setHidesBottomBarWhenPushed:YES];
NSMutableArray * array =[[NSMutableArray
alloc]initWithArray:self.navigationController.viewControllers];
    //删除最后一个,也就是自己
    [array
removeObjectAtIndex:array.count-1];
    //添加要跳转的controller
    [array
addObject:examineepersonal];
    [self.navigationController
setViewControllers:array animated:YES];

3、返回按钮为中文字,且字体会在最左边:
UIBarButtonItem * backItem = [[UIBarButtonItem alloc] init];
[backItem setTitle:@“返回”];
self.navigationItem.backBarButtonItem = backItem;

4、写了-(void)viewDidLoad{}就不要写-(void)aweakfromNib{
}

5、写上以下代码就可以让view controller的起始点自然在navigation bar下面:
if([self respondsToSelector:@selector(edgesExtenedLayout)]){
self.edgesForExtendedLayout =
UIRectEdgeNone
}

6、给textField加左边的view
[self addLeftViewToTextField:_emailTF];
-(void)addLeftViewToTextField:(UITextField *)textField{
CGrect frameLeft = [textField frame];
frameLeft.size.width = 10;
UIView * leftView = [[UIView alloc] initWithFrame:frameLeft];
[textField setLeftViewMode:UITextFieldViewModeAlways];
[textField setLeftView:leftView];
}

7、post数据json解析:
NSDictionary * retDic = [NSJSONSerialization JSONObjectWithData:operation.responseData options:NSJSONReadingMutableContainers error:nil];

8、HUD显示的方法:
MBProgressHUD *hud = [MBProgressHUD
showHUDAddedTo:this animated:YES];
hud.mode =
MBProgressHUDModeText;
            hud.labelText =
@"发送失败";
            hud.margin =
10.f;
            hud.yOffset =
150.f;
            hud.removeFromSuperViewOnHide =
YES;
            [hud
hide:YES
afterDelay:0.5];

9、将图片保存到沙盒目录里:

[self
saveImage:image withName:@"avarter_tmp.jpg"];
- (void) saveImage:(UIImage *)image withName:(NSString
*)name {
   
NSData *imageData = UIImageJPEGRepresentation(image,
0.5);
   
NSString *imagePath = [self
writeImage:imageData withName:name];
}

- (NSString *) writeImage:(NSData *)imageData withName:(NSString
*)name {
   
NSString *fullPath = [[NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"]
stringByAppendingPathComponent:name];
    [imageData
writeToFile:fullPath atomically:YES];
   
return fullPath;
}

10、cell点击之后闪一下:
[cell
setSelectionStyle:UITableViewCellSelectionStyleNone];

11、loadFromNib函数
+ (id)loadFromNib:(NSMutableDictionary *)_extraParams {
    
   
NSArray* arrayNib = [[NSBundle
mainBundle] loadNibNamed:@"KCShareViewController"
owner:self
options:nil];
   
return [arrayNib objectAtIndex:0];
}

12、友盟分享:
qq的URLScheme必须要转换为16机制。不然会出现很多问题。

13、给字符串加一条横线(富文本编辑)
 NSMutableAttributedString * testAttriString = [[NSMutableAttributedString
alloc] initWithString:[NSString
stringWithFormat:@“¥%@“,@“test”]];
            [testAttriString
addAttribute:NSStrikethroughStyleAttributeName
value:[NSNumber
numberWithInt:NSUnderlineStyleSingle]
range:NSMakeRange(0, testAttriString.length)];
           
_dayLabel.attributedText = testAttriString;

14、计算某一条字符串的size
CGSize size = [_MoneyLabel.text
sizeWithFont:[UIFont
systemFontOfSize:17]
constrainedToSize:CGSizeMake(MAXFLOAT,
23)];

15、让UIImageView里的图片加载完之后在过渡透明度显示图片
[_courseImage
sd_setImageWithURL:[NSURL
URLWithString:courseModel.picture]
placeholderImage:[UIImage
drawPlaceholderImage:_courseImage.size
colorRGB:courseModel.picture_color]
options:0
completed:^(UIImage *image,
NSError *error, SDImageCacheType cacheType,
NSURL *imageURL) {
        this.courseImage.alpha =
0.0;
        [UIView
animateWithDuration:0.3
animations:^{
            this.courseImage.alpha =
1.0;
        }];
        
    }];

15、cell上的倒计时
1)、首先、每cell上都有一个NSTimer在进行倒计时。

-(void)startTimer:(NSString *)str
{
   
_strId = str;
    [_timer
invalidate];
   
_timer = nil;
   
_timer = [NSTimer
scheduledTimerWithTimeInterval:1
                                             
target:self
selector:@selector(refreshTime)
                                           
userInfo:nil
repeats:YES];
    [[NSRunLoop
currentRunLoop] addTimer:_timer
forMode:UITrackingRunLoopMode];
}

-(void)refreshTime
{
   
if ([self.delegate
respondsToSelector:@selector(timeCountDownCell: withtvid:)]) {
        [self.delegate
timeCountDownCell:self
withtvid:_strId];
//这里的代理方法是对UI上的显示进行更新。
    }
}
2)、其次,在controller里对model里的时间进行倒计时。
-(void)allCourseInfomodelTimeCountdown
{
    [_timer
invalidate];
   
_timer = nil;
   
_timer= [NSTimer
scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(counttimedown)
userInfo:nil
repeats:YES];
    [[NSRunLoop
currentRunLoop] addTimer:_timer
forMode:UITrackingRunLoopMode];
}

-(void)counttimedown
{
   
if (self.courseLists.count!=0)
{
       
for (int i=0; i<self.courseLists.count;
i++) {
           
CourseInfo * model = [self.courseLists
objectAtIndex:i];
           
if (model.discount_timelimit&&[model.discount_timelimit
integerValue]!=0) {
                model.discount_timelimit =[NSNumber
numberWithInt:([model.discount_timelimit
intValue]-1)];
            }
        }
    }
}
3)、实现UI更新的代理方法
-(void)timeCountDownCell:(CourseCell *)cell withtvid:(NSString
*)strId
{
}

16、CHDViewController这个是左右滚动的三方库

17、ZHPickView这个是选择器的三方库

18、获取到手势点击的位置而触发不同的函数
-(void)SingleTap:(UIGestureRecognizer *)gestureRecognizer
{
   
CGPoint point = [gestureRecognizer
locationInView:self.tableview];
   
NSIndexPath *indexPath = [self.tableview
indexPathForRowAtPoint:point];
    
   
// Get the whole rect for section.
   
CGRect sectionRect = [self.tableview
rectForSection:[indexPath
section]];
    
   
// Get the rect of the section header in specified section.
   
CGRect sectionHeaderRect = [self.tableview
rectForHeaderInSection:[indexPath
section]];
    
   
// Calculate the actual rect for all cells.
   
CGRect croppedRect = CGRectMake(sectionRect.origin.x, sectionRect.origin.y+sectionHeaderRect.size.height,
sectionRect.size.width, sectionRect.size.height-sectionHeaderRect.size.height);
    
   
if (CGRectContainsPoint(croppedRect, point)) {
       
// If we tapped at cell part.
       
if ([indexPath section] !=
3) {
            [self.audioview
removeCurrentView];
           
self.tableviewspacetobottom.constant =
0;
            [self
tableView:self.tableview
didSelectRowAtIndexPath:indexPath];
        }
else {
            [self
tableView:self.tableview
didSelectRowAtIndexPath:indexPath];
        }
    }
else {
       
// if we tapped at section header, we will not do the select.
        [self.audioview
removeCurrentView];
       
self.tableviewspacetobottom.constant =
0;
    }
}

19、//将图片固定像素
- (UIImage *) scaleFromImage: (UIImage *) image toSize: (CGSize)
size
{
   
UIGraphicsBeginImageContext(size);
    [image
drawInRect:CGRectMake(0,
0, size.width, size.height)];
   
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
   
UIGraphicsEndImageContext();
   
return newImage;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS 函数