navigationBar变为纯透明
//第一种方法----导航栏纯透明-要写在UINavigationController控制器或其子类
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//去掉导航栏底部的黑线
self.navigationBar.shadowImage = [UIImage new];
tabBar变为纯透明---要写在UITabBarController控制器或其子类
//第一种方法----导航栏纯透明
[self.tabBar setBackgroundImage:[UIImage new]];
//去掉导航栏底部的黑线
self.tabBar.shadowImage = [UIImage new];
navigationBar根据滑动距离,其渐变色的实现
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
UIScrollView* scrollView = [[UIScrollView alloc]init];
[self.view addSubview:scrollView];
scrollView.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 120);
scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 360);
scrollView.backgroundColor = [UIColor yellowColor];
scrollView.delegate = self;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
第一种方法
// CGFloat offsetToShow = 200.0;
// CGFloat alpha = 1 - (offsetToShow -scrollView.contentOffset.y)/offsetToShow;
// [[self.navigationController.navigationBar subviews]objectAtIndex:0].alpha = alpha;
第二种方法
CGFloat offsetToShow = 200.0;
CGFloat alpha = 1 - (offsetToShow -scrollView.contentOffset.y)/offsetToShow;
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor orangeColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
}
//
#pragma mark----生成一张纯色的图片
- (UIImage *)imageWithColor:(UIColor *)color{
CGRect rect = CGRectMake(0.0f, 0.0, 1.0, 1.0);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndPDFContext();
return image;
}
如何改变navigationItem的BarButtonItem位置:
一般情况下,右边的item会和屏幕右侧保持一段距离 。可以通过添加一个负值宽度的固定间距的item来解决,也可以改变宽度实现不同的间隔
UIImage* image = [[UIImage imageNamed:@"tabbar_life"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//宽度为负数的固定间距的系统item
UIBarButtonItem* rightNegativeSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[rightNegativeSpace setWidth:-15];//靠近右边界
UIBarButtonItem *rightBtnItem1 = [[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonItemClicked:)];
self.navigationItem.rightBarButtonItems = @[rightNegativeSpace,rightBtnItem1];
UIImage 占用内存大小
UIImage* image = [UIImage imageNamed:@"tabbar_user"];
NSUInteger size = CGImageGetHeight(image.CGImage) * CGImageGetBytesPerRow(image.CGImage);
取上整与取下整
//floor(x),有时候也写做Floor(x),其功能是“下取整”,即取不大于x的最大整数
double x = 3.14;
//floor(x)为3;
double y = 9.9999;
// floor(y)为9
// ceil函数的作用是求不小于给定实数的最小整数。
// ceil(x)为4
// ceil(y)为10
防止scrollView手势覆盖侧滑手势
[scrollView.panGestureRecognizerrequireGestureRecognizerToFail:self.navigationController.interactivePopGestureRecognizer];
UITextField每四位加一个空格,实现代理
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// 四位加一个空格
if ([string isEqualToString:@""])
{
// 删除字符
if ((textField.text.length - 2) % 5 == 0)
{
textField.text = [textField.text substringToIndex:textField.text.length - 1];
}
return YES;
}
else
{
if (textField.text.length % 5 == 0)
{
textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
}
}
return YES;
}
两种方法删除NSUserDefaults所有记录
//方法一
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
//方法二
- (void)resetDefaults
{
NSUserDefaults * defs = [NSUserDefaults standardUserDefaults];
NSDictionary * dict = [defs dictionaryRepresentation];
for (id key in dict)
{
[defs removeObjectForKey:key];
}
[defs synchronize];
}
+ (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;
}
NSArray 快速求总和 最大值 最小值 和 平均值
NSArray *array = [NSArray arrayWithObjects:@"2.0", @"2.3", @"3.0", @"4.0", @"10", nil];
CGFloat sum = [[array valueForKeyPath:@"@sum.floatValue"] floatValue];
CGFloat avg = [[array valueForKeyPath:@"@avg.floatValue"] floatValue];
CGFloat max =[[array valueForKeyPath:@"@max.floatValue"] floatValue];
CGFloat min =[[array valueForKeyPath:@"@min.floatValue"] floatValue];
NSLog(@"%f\n%f\n%f\n%f",sum,avg,max,min);
关于内存泄漏的注意点
在使用图片资源的时候,少使用imageNamed:方法去获取使用频次不高的图片资源。因为使用imageNamed:加载的图片资源会一直存在内存里面,对内存的浪费也是巨大的。
网友评论