退出APP到桌面
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIWindow *window = app.window;
[UIView animateWithDuration:1.0f animations:^{
window.alpha = 0;
window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
} completion:^(BOOL finished) {
exit(0);
}];
写在父类控制器的方法(个人习惯)
// 永不待机
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
// 布局从不从navigation开始
self.edgesForExtendedLayout = UIRectEdgeNone;
//适配iOS13以上的深色模式
-(UIColor *)colorWithDarkModeColor:(UIColor *)darkColor normalColor:(UIColor *)color
{
if (@available(iOS 13.0,*)) {
UIColor *dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trainCollection) {
if ([trainCollection userInterfaceStyle] == UIUserInterfaceStyleDark) {
return darkColor;
}
else {
return color;
}
}];
return dyColor;
}
return color;
}
控制器之间的操作
//使用APP外的浏览器打开URL
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
//关闭左滑回到上一页
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
//切换根窗口。一般用于退出账号
UIWindow *window = [[[UIApplication sharedApplication] delegate] window]; // 获得根窗口
Class controllerClass = NSClassFromString(@"LoginViewController");
BasicController *loginVC=[[controllerClass alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
window.rootViewController = nav;
//present跳转是覆盖全屏
controller.modalPresentationStyle = UIModalPresentationFullScreen;
//根据名称获取控制器
Class controllerClass = NSClassFromString(“NameViewController”);
BasicController *vc=[controllerClass new];
//获取控同一个navigation下的每一级跳转的控制器
NSMutableArray *vcArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
BasicController *control=vcArray.lastObject;
//删除某一个控制器并且重置跳转级别
int index = (int)[vcArray indexOfObject:self];
[vcArray removeObjectAtIndex:index];
[self.navigationController setViewControllers:vcArray animated:NO];
// 当一个视图控制器从视图控制器容器中被添加或者被删除之前,该方法被调用
- (void)willMoveToParentViewController:(UIViewController *)parent
// 当从一个视图控制容器中添加或者移除viewController后,该方法被调用
- (void)didMoveToParentViewController:(UIViewController *)paren
//返回的是一个UIView,是用来寻找最终哪一个视图来响应这个事件
- (UIView*)hitTest:(CGPoint)pointwithEvent:(UIEvent*)event;
//判断某一个点击的位置是否在视图范围内,如果在就返回YES。
- (BOOL)pointInside:(CGPoint)pointwithEvent:(UIEvent*)event;
//事件穿透
//视图覆盖的情况 想要透过上层视图的响应事件 响应下面的事件
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == 上层视图view) {
return 想要响应的下层视图view;
}
return hitView;
}
//视图覆盖 但是不想要响应上层视图view事件
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == 不想要响应视图的view) {
return nil; // 此处返回空即不相应任何事件
}
return hitView;
}
//让超出父视图范围的子视图响应事件
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
for (UIView *subView in self.subviews) {
CGPoint tp = [subView convertPoint:point fromView:self];
if (CGRectContainsPoint(subView.bounds, tp)) {
view = subView;
}
}
}
return view;
}
scrollview:
//滑动到指定位置
[self.tableview setContentOffset:CGPointMake(0, 100) animated:YES];
//或者
self.downCollectionView.contentOffset = CGPointMake(0, 100);
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if(!decelerate){
// NSLog(@"手指离开屏幕并且停止了滚动");
}
}
tableview:
//去掉留白
if (@available(iOS 11.0, *)) {
self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
//关闭预估高度
self.tableview.estimatedRowHeight = 0;
self.tableview.estimatedSectionHeaderHeight = 0;
self.tableview.estimatedSectionFooterHeight = 0;
//没有数据的地方不显示分割线
self.tableview.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
//刷新某个rows
NSIndexPath *path=[NSIndexPath indexPathForRow:0 inSection:0];
[self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObjects:path, nil] withRowAnimation:UITableViewRowAnimationNone];
//刷新某个section
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:0];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
//使分割线顶到左边
- (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];
}
if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
[cell setPreservesSuperviewLayoutMargins:NO];
}
}
//隐藏某个cell的分割线
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, screenWidth);
//自适应高度
self.tableView.estimatedRowHeight =10;
self.tableView.rowHeight=UITableViewAutomaticDimension;
//避免cell复用导致混乱
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", indexPath.section, indexPath.row];//以indexPath来唯一确定cell
nameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
if (cell == nil) {
cell = [[nameTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//获取某个cell
NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow];
nameTableView *cell=[self.tableView cellForRowAtIndexPath:indexPath];
//滑动到某一个cell
NSIndexPath *optionIndexPath=[NSIndexPath indexPathForRow:0 inSection:2];
//获取某个cell的frame
CGRect cellrect = [self.tableView rectForRowAtIndexPath:optionIndexPath];
[self.tableView setContentOffset:CGPointMake(0, cellrect.origin.y) animated:YES];
//cell点击时不会变灰
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell点击时颜色变深,手指松开恢复
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
//以下两种写法效果相同,获取子级的子级控件在第一级父视图中的frame
CGRect rect=[cell convertRect:cell.label.bounds toView:self.tableView];
CGRect rect = [self.tableView convertRect:cell.label.bounds fromView:cell];
//以下两种写法效果相同,获取子级的子级控件在第一级父视图中的坐标
CGPoint point = [cell convertPoint:cell.label.bounds.origin toView:self.tableView]
CGPoint point = [self.tableView convertPoint:cell.label.bounds.origin fromView:cell]
collectionview:
//禁止复用
NSString *identifier=[NSString stringWithFormat:@"%ld%ld",(long)indexPath.section,(long)indexPath.row];
[self.collection registerClass:[NameCollectionViewCell class] forCellWithReuseIdentifier:identifier];
NameCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//默认点击cell
[collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:2 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
//获取某个cell
NameCollectionViewCell *cell = (NameCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//改变选中状态的颜色,在UICollectionViewCell文件中
- (void)setSelected:(BOOL)selected{
[super setSelected:selected];
if (selected) {
if (self.redTxtBool==NO) {
}else{
self.txtLabel.textColor=[UIColor blackclor];
}
//选中时
}else{
//非选中
self.txtLabel.textColor=[UIColor redcolor];
}
}
网友评论