1.在自定义View中跳转到其他控制器
[[self viewController].navigationController pushViewController:allPatientVC animated:YES];
- (UIViewController *)viewController {
for (UIView* next = [self superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
2.设置UITableView分割线位置
//1.调整(iOS7以上)表格分隔线边距
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
self.tableView.separatorInset = UIEdgeInsetsZero;
}
//2.调整(iOS8以上)view边距(或者在cell中设置preservesSuperviewLayoutMargins,二者等效)
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
self.tableView.layoutMargins = UIEdgeInsetsZero;
}
3.修改UITextField的占位符的字体颜色和大小
方法1:利用富文本
@property (weak, nonatomic) IBOutlet UITextField *textField;
NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor magentaColor], NSFontAttributeName:[UIFont systemFontOfSize:15]};
self.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"欢迎回来" attributes:dic];
self.textField.tintColor = [UIColor cyanColor];
方法2:KVC
self.textField.placeholder = @"欢迎回来!";
[self.textField setValue:[UIColor magentaColor] forKeyPath:@"_placeholderLabel.textColor"]
[self.textField setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
self.textField.tintColor = [UIColor greenColor];
4.修改导航栏item的左右间距
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
rightSpace.width = -7;
self.navigationItem.rightBarButtonItems = @[rightSpace,rightItem];
5.UIButton 左边图片,右边文字
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 100, 50);
button.backgroundColor = [UIColor clearColor];
//设置button正常状态下的图片
[button setImage:[UIImage imageNamed:@"_star_normal.png"] forState:UIControlStateNormal];
//设置button高亮状态下的图片
[button setImage:[UIImage imageNamed:@"_star_highlighted.png"] forState:UIControlStateHighlighted];
//设置button正常状态下的背景图
[button setBackgroundImage:[UIImage imageNamed:@"_normal.png"] forState:UIControlStateNormal];
//设置button高亮状态下的背景图
[button setBackgroundImage:[UIImage imageNamed:@"_highlighted.png"] forState:UIControlStateHighlighted];
//button图片的偏移量,距上左下右分别(10, 10, 10, 60)像素点
button.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 60);
[button setTitle:@"南瓜瓜" forState:UIControlStateNormal];
//button标题的偏移量,这个偏移量是相对于图片的
button.titleEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
//设置button正常状态下的标题颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//设置button高亮状态下的标题颜色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
button.titleLabel.font = [UIFont systemFontOfSize:14];
[self.view addSubview:button];
6.检查字符串是否为url
- (NSString *)getCompleteWebsite:(NSString *)urlStr{
NSString *returnUrlStr = nil;
NSString *scheme = nil;
assert(urlStr != nil);
urlStr = [urlStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if ( (urlStr != nil) && (urlStr.length != 0) ) {
NSRange urlRange = [urlStr rangeOfString:@"://"];
if (urlRange.location == NSNotFound) {
returnUrlStr = [NSString stringWithFormat:@"http://%@", urlStr];
} else {
scheme = [urlStr substringWithRange:NSMakeRange(0, urlRange.location)];
assert(scheme != nil);
if ( ([scheme compare:@"http" options:NSCaseInsensitiveSearch] == NSOrderedSame)
|| ([scheme compare:@"https" options:NSCaseInsensitiveSearch] == NSOrderedSame) ) {
returnUrlStr = urlStr;
} else {
//不支持的URL方案
}
}
}
return returnUrlStr;
}
7.秒转换为时分秒
-(NSString *)getMMSSFromSS:(NSString *)totalTime{
NSInteger seconds = [totalTime integerValue];
//format of hour
NSString *str_hour = [NSString stringWithFormat:@"%02ld",seconds/3600];
//format of minute
NSString *str_minute = [NSString stringWithFormat:@"%02ld",(seconds%3600)/60];
//format of second
NSString *str_second = [NSString stringWithFormat:@"%02ld",seconds%60];
//format of time
NSString *format_time = [NSString stringWithFormat:@"%@:%@:%@",str_hour,str_minute,str_second];
return format_time;
}`
8.禁用系统左滑返回
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 禁用返回手势
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// 开启返回手势
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}
9.判断UIImage图片是否是同一张图片(在写UIImagePickerController中要判断之前的image和当下选择的uiimage是不是一个image)
UIImage* image= info[UIImagePickerControllerOriginalImage];
UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(self.imageViews.count*80.0, 0, 80, 80)];
imageView.image=image;
for (UIImageView* subimage in self.imageViews) {
if ([UIImagePNGRepresentation(subimage.image) isEqual:UIImagePNGRepresentation(image)]) {
UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@" 此图片已经选择过,选择其他图片" message:@" 此图片已经选择过,选择其他图片" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok ", nil];
[alert show];
return;
}
}
10.真色彩显示
真彩色的显示会根据光感应器来自动的调节达到特定环境下显示与性能的平衡效果,如果需要这个功能的话,可以在info.plist里配置(在Source Code模式下):
<key>UIWhitePointAdaptivityStyle</key>
它有五种取值,分别是:
<string>UIWhitePointAdaptivityStyleStandard</string> // 标准模式
<string>UIWhitePointAdaptivityStyleReading</string> // 阅读模式
<string>UIWhitePointAdaptivityStylePhoto</string> // 图片模式
<string>UIWhitePointAdaptivityStyleVideo</string> // 视频模式
<string>UIWhitePointAdaptivityStyleStandard</string> // 游戏模式
也就是说如果你的项目是阅读类的,就选择UIWhitePointAdaptivityStyleReading这个模式,五种模式的显示效果是从上往下递减,也就是说如果你的项目是图片处理类的,你选择的是阅读模式,给选择太好的效果会影响性能.
11.UIRefreshControl
在iOS 10 中, UIRefreshControl可以直接在UICollectionView和UITableView中使用,并且脱离了UITableViewController.现在RefreshControl是UIScrollView的一个属性.
使用方法:
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
collectionView.refreshControl = refreshControl;
12 UIImage图片转成Base64字符串:
UIImage *originImage = [UIImage imageNamed:@"Cover.png"];
NSData *data = UIImageJPEGRepresentation(originImage, 1.0f);
NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSLog(@"encodedImageStr==%@",encodedImageStr);
//Base64字符串转UIImage图片:
NSData *decodedImageData = [[NSData alloc]initWithBase64EncodedString:encodedImageStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 100, 200, 400)];
[imgView setImage:decodedImage];
[self.view addSubview:imgView];
NSLog(@"decodedImage==%@",decodedImageData);
13.iOS如何快速得到数组所有元素累加结果,平均值和最大最小值
NSArray *values = @[@72, @78, @75, @70, @72, @73, @77, @78, @75, @70, @72, @73, @87, @78, @75, @70, @72];
NSNumber *avg = [values valueForKeyPath:@"@avg.self"];
NSNumber *sum = [values valueForKeyPath:@"@sum.self"];
NSNumber *max = [values valueForKeyPath:@"@max.self"];
NSNumber *min = [values valueForKeyPath:@"@min.self"];
14.隐藏导航栏
@interface WLHomePageController () <UINavigationControllerDelegate>
@end
@implementation WLHomePageController
#pragma mark - lifeCycle
- (void)viewDidLoad {
[super viewDidLoad];
// 设置导航控制器的代理为self
self.navigationController.delegate = self;
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 判断要显示的控制器是否是自己
BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
[self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
- (void)dealloc {
self.navigationController.delegate = nil;
}
15.防止scrollView手势覆盖侧滑手势
[scrollView.panGestureRecognizerrequireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
16.字符串中是否含有中文
+ (BOOL)checkIsChinese:(NSString *)string
{
for (int i=0; i<string.length; i++)
{
unichar ch = [string characterAtIndex:i];
if (0x4E00 <= ch && ch <= 0x9FA5)
{
return YES;
}
}
return NO;
}
网友评论