拍照调用前置摄像头
imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
1、修改 UITextfield 的 Placeholder字体的颜色方法如下:
- [_textField setValue:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
2、自定义nav 返回按钮导致右滑返回失效
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
3、ios8 毛玻璃效果
- // 图片
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 400)];
- [imageView setImage:[UIImage imageNamed:@"IMG_0015.JPG"]];
- [self.view addSubview:imageView];
- // blur效果
- self.visualEfView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
- _visualEfView.frame = CGRectMake(0, 0, 300, 400);
- _visualEfView.alpha = 1.0;
- [imageView addSubview:_visualEfView];
3、
iOS开发- iOS7显示偏差(UITableView下移)解决办法
iOS7在Conttoller中新增了这个属性:
automaticallyAdjustsScrollViewInsets,当设置为YES时(默认YES),如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,这样可以让scroll占据整个视图,又不会让导航栏遮盖。
我们设置automaticallyAdjustsScrollViewInsets这个属性为no,就可以解决这个问题。
如下:
当然也可以通过修改UIViewController的edgesForExtendedLayout这个属性来实现。
self.edgesForExtendedLayout = UIExtendedEdgeNone;
TableView不显示没内容的Cell怎么办?
类似这种,我不想让下面那些空的显示.
很简单.
self.tableView.tableFooterView = [[UIView alloc] init];
试过的都说好.
加完这句之后就变成了这样.
自定义了leftBarbuttonItem左滑返回手势失效了怎么办?
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:img
style:UIBarButtonItemStylePlain
target:self
action:@selector(onBack:)];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
ScrollView莫名其妙不能在viewController划到顶怎么办?
self.automaticallyAdjustsScrollViewInsets = NO;
键盘事件写的好烦躁,都想摔键盘了,怎么办?
- 买个结实的键盘.
- 使用IQKeyboardManager(github上可搜索),用完之后腰也不疼了,腿也不酸了.
为什么我的app老是不流畅,到底哪里出了问题?
如图
这个神器叫做:KMCGeigerCounter
快去github搬运吧.
怎么在不新建一个Cell的情况下调整separaLine的位置?
_myTableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);
怎么点击self.view就让键盘收起,需要添加一个tapGestures么?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
怎么给每个ViewController设定默认的背景图片?
使用基类啊,少年.
想在代码里改在xib里添加的layoutAttributes,但是怎么用代码找啊?
像拉button一样的拉你的约束.nslayoutattribute也是可以拉线的.
怎么像safari一样滑动的时候隐藏navigationbar?
navigationController.hidesBarsOnSwipe = Yes
导航条返回键带的title太讨厌了,怎么让它消失!
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
CoreData用起来好烦,语法又臭又长,怎么办?
MagicRecord
CollectionView 怎么实现tableview那种悬停的header?
CSStickyHeaderFlowLayout
if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTranslucent:NO];
}
else
{
[[UINavigationBar appearance] setTintColor:[UIColor orangeColor]];
}
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
/**
* 改变后需要及时刷新的调用
*/
[selfsetNeedsStatusBarAppearanceUpdate];
UITabBarItem *tabBarItem1=self.tabBar.items[0];
UITabBarItem *tabBarItem2=self.tabBar.items[1];
// tabBarItem1.selectedImage=[UIImage imageNamed:@"iconAccount"];
// tabBarItem1.image=[UIImage imageNamed:@"iconAccountOn"];
tabBarItem1.image = [[UIImageimageNamed:@"iconAccount"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1.selectedImage = [[UIImageimageNamed:@"iconAccountOn"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem2.image = [[UIImageimageNamed:@"iconFortune"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem2.selectedImage = [[UIImageimageNamed:@"iconFortuneOn"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// tabBarItem2.selectedImage=[UIImage imageNamed:@"iconFortune"];
// tabBarItem2.image=[UIImage imageNamed:@"iconFortuneOn"];
self.tabBar.tintColor=[UIColorcolorWithRed:255/255green:56/255blue:0alpha:1.0];
原因:
ios7的时候在storyboard 设置 TableView的separator intend = 0 可以让tableview的分割条顶到头。
但是,升级了iOS8时,发现不起作用了。iOS8 在cell和tableview中都引入了layoutMargins属性,而且这个属性在iOS 7中并没有,所以你需要区别对待这两个版本。
解决办法如下:
- -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
- [cell setSeparatorInset:UIEdgeInsetsZero];
- }
-
- if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
- [cell setLayoutMargins:UIEdgeInsetsZero];
- }
-
- }
- -(void)viewDidLayoutSubviews
- {
- [super viewDidLayoutSubviews];
-
- if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
- [self.tableView setSeparatorInset:UIEdgeInsetsZero];
- }
-
- if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
- [self.tableView setLayoutMargins:UIEdgeInsetsZero];
- }
- }
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[selfanimateTextField: textFieldup:YES];
}
//在UITextField编辑完成调用方法
- (void)textFieldDidEndEditing:(UITextField *)textField{
[selfanimateTextField: textFieldup:NO];
}
//视图上移的方法
- (void) animateTextField: (UITextField *) textField up: (BOOL) up
{ //设置视图上移的距离,单位像素
constint movementDistance =RISE_HEIGHT;
// tweak as needed
//三目运算,判定是否需要上移视图或者不变
int movement = (up ? -movementDistance : movementDistance);
//设置动画的名字
[UIViewbeginAnimations:@"Animation"context:nil];
//设置动画的开始移动位置
[UIViewsetAnimationBeginsFromCurrentState:YES];
//设置动画的间隔时间
[UIViewsetAnimationDuration:0.20];
//设置视图移动的位移
self.view.frame =CGRectOffset(self.view.frame,0, movement);
//设置动画结束
[UIViewcommitAnimations];
}
在UITableView上添加了UITapGestureRecognizer后会导致didSelectRowAtIndexPath失效,原因是UITapGestureRecognizer会截取了tableView的touch事件,导致无法响应行选择,解决方法是重写UIGestureRecognizerDelegate中的
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//如果当前是tableView
//做自己想做的事
return NO;
}
return YES;
}
在ios7中,UITableViewCell左侧会有默认15像素的空白。这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉。
但是在ios8中,设置setSeparatorInset:UIEdgeInsetsZero 已经不起作用了。下面是解决办法
首先在viewDidLoad方法加入以下代码:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
然后在UITableView的代理方法中加入以下代码
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
这样,空白就没有了
判断tableviewcell在可视区域的几种方法
1.
- (NSArray*)visibleCells;
UITableview的方法,这个最直接,返回一个UITableviewcell的数组。
对于自定制的cell,之后的处理可能稍微繁琐些。
2.
- (NSArray*)indexPathsForVisibleRows;
UITableview的又一个方法,这个比较好用了,返回一个NSIndexPath的数组,可以直接用indexpath.row去调你的table_related_Array里的数据了。比较方便用于自定制的cell。
3.
- (CGRect)rectForRowAtIndexPath:(NSIndexPath*)indexPath;
CGRect cellR = [myTV
rectForRowAtIndexPath:indx
];
如果 myTV.contentOffset.y - cellR.origin.y < myCell.frame.size.height
或者 cellR.origin.y - myTV.contentOffset.y >myTV.size.height
这个时候myCell应该是不在myTV的可视区域了。
这个方法可以用在代理回调较多的设计中。
另:
1和2在自动根据数据伸长的cell中好像不太准确。
-(UIViewController *)custmerController
{
UIViewController *result =nil;
UIWindow * window = [[UIApplicationsharedApplication]keyWindow];
if (window.windowLevel !=UIWindowLevelNormal)
{
NSArray *windows = [[UIApplicationsharedApplication]windows];
for(UIWindow * tmpWinin windows)
{
if (tmpWin.windowLevel ==UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView *frontView = [[windowsubviews]objectAtIndex:0];
id nextResponder = [frontViewnextResponder];
if ([nextResponderisKindOfClass:[UIViewControllerclass]])
result = nextResponder;
else
result = window.rootViewController;
if (result.presentedViewController) {
result=result.presentedViewController;
}
return result;
}
在项目开发中,我们经常要用到UISearchBar,在网上看到了很多关于去除掉他背景色的方法,都已经失效了,今天来分享一个正常使用的方法,希望能帮到大家
今天用到UISearchBar,之前网上提供的方法已经不能有效的去除掉它的背景色了,修改背景色方法如下:
mySearchBar.backgroundColor =RGBACOLOR(249,249,249,1);
mySearchBar.backgroundImage= [self imageWithColor:[UIColor clearColor]size:mySearchBar.bounds.size];
//取消searchbar背景色
- (UIImage *)imageWithColor:(UIColor *)colorsize:(CGSize)size
{
CGRectrect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageCont ext(rect.size);
CGContextRefcontext = UIGraphicsGetCurrentCont ext();
CGContextSetFillColorWit hColor(context,[color CGColor]);
CGContextFillRect(context,rect);
UIImage*image = UIGraphicsGetImageFromCu rrentImageContext();
UIGraphicsEndImageContex t();
return
image;
}
iOS7在Conttoller中新增了这个属性:
automaticallyAdjustsScrollViewInsets,当设置为YES时(默认YES),如果视图里面存在唯一一个UIScrollView或其子类View,那么它会自动设置相应的内边距,这样可以让scroll占据整个视图,又不会让导航栏遮盖。
我们设置automaticallyAdjustsScrollViewInsets这个属性为no,就可以解决这个问题。
如下:
当然也可以通过修改UIViewController的edgesForExtendedLayout这个属性来实现。
self.edgesForExtendedLayout = UIExtendedEdgeNone;TableView不显示没内容的Cell怎么办?
类似这种,我不想让下面那些空的显示.
很简单.
self.tableView.tableFooterView = [[UIView alloc] init];
试过的都说好.
加完这句之后就变成了这样.
自定义了leftBarbuttonItem左滑返回手势失效了怎么办?
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithImage:img
style:UIBarButtonItemStylePlain
target:self
action:@selector(onBack:)];
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
ScrollView莫名其妙不能在viewController划到顶怎么办?
self.automaticallyAdjustsScrollViewInsets = NO;
键盘事件写的好烦躁,都想摔键盘了,怎么办?
- 买个结实的键盘.
- 使用IQKeyboardManager(github上可搜索),用完之后腰也不疼了,腿也不酸了.
为什么我的app老是不流畅,到底哪里出了问题?
如图
这个神器叫做:KMCGeigerCounter
快去github搬运吧.
怎么在不新建一个Cell的情况下调整separaLine的位置?
_myTableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);
怎么点击self.view就让键盘收起,需要添加一个tapGestures么?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
怎么给每个ViewController设定默认的背景图片?
使用基类啊,少年.
想在代码里改在xib里添加的layoutAttributes,但是怎么用代码找啊?
像拉button一样的拉你的约束.nslayoutattribute也是可以拉线的.
怎么像safari一样滑动的时候隐藏navigationbar?
navigationController.hidesBarsOnSwipe = Yes
导航条返回键带的title太讨厌了,怎么让它消失!
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
CoreData用起来好烦,语法又臭又长,怎么办?
MagicRecord
CollectionView 怎么实现tableview那种悬停的header?
CSStickyHeaderFlowLayout
if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTranslucent:NO];
}
else
{
[[UINavigationBar appearance] setTintColor:[UIColor orangeColor]];
}
[[UIApplicationsharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
/**
* 改变后需要及时刷新的调用
*/
[selfsetNeedsStatusBarAppearanceUpdate];
UITabBarItem *tabBarItem1=self.tabBar.items[0];
UITabBarItem *tabBarItem2=self.tabBar.items[1];
// tabBarItem1.selectedImage=[UIImage imageNamed:@"iconAccount"];
// tabBarItem1.image=[UIImage imageNamed:@"iconAccountOn"];
tabBarItem1.image = [[UIImageimageNamed:@"iconAccount"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1.selectedImage = [[UIImageimageNamed:@"iconAccountOn"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem2.image = [[UIImageimageNamed:@"iconFortune"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem2.selectedImage = [[UIImageimageNamed:@"iconFortuneOn"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// tabBarItem2.selectedImage=[UIImage imageNamed:@"iconFortune"];
// tabBarItem2.image=[UIImage imageNamed:@"iconFortuneOn"];
self.tabBar.tintColor=[UIColorcolorWithRed:255/255green:56/255blue:0alpha:1.0];
ios7的时候在storyboard 设置 TableView的separator intend = 0 可以让tableview的分割条顶到头。
但是,升级了iOS8时,发现不起作用了。iOS8 在cell和tableview中都引入了layoutMargins属性,而且这个属性在iOS 7中并没有,所以你需要区别对待这两个版本。
解决办法如下:
- -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
- [cell setSeparatorInset:UIEdgeInsetsZero];
- }
- if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
- [cell setLayoutMargins:UIEdgeInsetsZero];
- }
- }
- -(void)viewDidLayoutSubviews
- {
- [super viewDidLayoutSubviews];
- if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
- [self.tableView setSeparatorInset:UIEdgeInsetsZero];
- }
- if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
- [self.tableView setLayoutMargins:UIEdgeInsetsZero];
- }
- }
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[selfanimateTextField: textFieldup:YES];
}
//在UITextField编辑完成调用方法
- (void)textFieldDidEndEditing:(UITextField *)textField{
[selfanimateTextField: textFieldup:NO];
}
//视图上移的方法
- (void) animateTextField: (UITextField *) textField up: (BOOL) up
{ //设置视图上移的距离,单位像素
constint movementDistance =RISE_HEIGHT;
// tweak as needed
//三目运算,判定是否需要上移视图或者不变
int movement = (up ? -movementDistance : movementDistance);
//设置动画的名字
[UIViewbeginAnimations:@"Animation"context:nil];
//设置动画的开始移动位置
[UIViewsetAnimationBeginsFromCurrentState:YES];
//设置动画的间隔时间
[UIViewsetAnimationDuration:0.20];
//设置视图移动的位移
self.view.frame =CGRectOffset(self.view.frame,0, movement);
//设置动画结束
[UIViewcommitAnimations];
}
{
if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {//如果当前是tableView
//做自己想做的事
return NO;
}
return YES;
}
在ios7中,UITableViewCell左侧会有默认15像素的空白。这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉。
但是在ios8中,设置setSeparatorInset:UIEdgeInsetsZero 已经不起作用了。下面是解决办法
首先在viewDidLoad方法加入以下代码:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
然后在UITableView的代理方法中加入以下代码- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
这样,空白就没有了判断tableviewcell在可视区域的几种方法
UITableview的方法,这个最直接,返回一个UITableviewcell的数组。
对于自定制的cell,之后的处理可能稍微繁琐些。
2. - (NSArray*)indexPathsForVisibleRows;
UITableview的又一个方法,这个比较好用了,返回一个NSIndexPath的数组,可以直接用indexpath.row去调你的table_related_Array里的数据了。比较方便用于自定制的cell。
3. - (CGRect)rectForRowAtIndexPath:(NSIndexPath*)indexPath;
CGRect cellR = [myTV rectForRowAtIndexPath:indx ];
如果 myTV.contentOffset.y - cellR.origin.y < myCell.frame.size.height
或者 cellR.origin.y - myTV.contentOffset.y >myTV.size.height
这个时候myCell应该是不在myTV的可视区域了。
这个方法可以用在代理回调较多的设计中。
另:
1和2在自动根据数据伸长的cell中好像不太准确。
-(UIViewController *)custmerController
{
UIViewController *result =nil;
UIWindow * window = [[UIApplicationsharedApplication]keyWindow];
if (window.windowLevel !=UIWindowLevelNormal)
{
NSArray *windows = [[UIApplicationsharedApplication]windows];
for(UIWindow * tmpWinin windows)
{
if (tmpWin.windowLevel ==UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView *frontView = [[windowsubviews]objectAtIndex:0];
id nextResponder = [frontViewnextResponder];
if ([nextResponderisKindOfClass:[UIViewControllerclass]])
result = nextResponder;
else
result = window.rootViewController;
if (result.presentedViewController) {
result=result.presentedViewController;
}
return result;
}
在项目开发中,我们经常要用到UISearchBar,在网上看到了很多关于去除掉他背景色的方法,都已经失效了,今天来分享一个正常使用的方法,希望能帮到大家
今天用到UISearchBar,之前网上提供的方法已经不能有效的去除掉它的背景色了,修改背景色方法如下:
mySearchBar.backgroundColor =RGBACOLOR(249,249,249,1);
mySearchBar.backgroundImage= [self imageWithColor:[UIColor clearColor]size:mySearchBar.bounds.size];
//取消searchbar背景色
- (UIImage *)imageWithColor:(UIColor *)colorsize:(CGSize)size
{
CGRectrect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageCont ext(rect.size);
CGContextRefcontext = UIGraphicsGetCurrentCont ext();
CGContextSetFillColorWit hColor(context,[color CGColor]);
CGContextFillRect(context,rect);
UIImage*image = UIGraphicsGetImageFromCu rrentImageContext();
UIGraphicsEndImageContex t();
return
image;
}