iOS学习资料十一之View

1.删除某个view所有的子视图

[[someView subviews]

makeObjectsPerformSelector:@selector(removeFromSuperview)];

2.删除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];

}

// 方法三

[[NSUserDefaults standardUserDefaults] setPersistentDomain:[NSDictionary dictionary] forName:[[NSBundle mainBundle] bundleIdentifier]];

3.禁用系统滑动返回功能

- (void)viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {self.navigationController.interactivePopGestureRecognizer.delegate = self;

}

}

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {self.navigationController.interactivePopGestureRecognizer.delegate = nil;

}

}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer

{

return NO;

}

4.模拟器报错


解决办法:

打开模拟器->Simulator->Reset Content and Settings...

如果不行,就重启试试!

5.自定义cell选中背景颜色

UIView *bgColorView = [[UIView alloc] init];

bgColorView.backgroundColor = [UIColor redColor];

[cell setSelectedBackgroundView:bgColorView];

6.UIView背景颜色渐变

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];

[self.view addSubview:view];

CAGradientLayer *gradient = [CAGradientLayer layer];

gradient.frame = view.bounds;

gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];

[view.layer insertSublayer:gradient atIndex:0];

7.停止UIView动画

[yourView.layer removeAllAnimations]

8.为UIView某个角添加圆角

// 左上角和右下角添加圆角

UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(20, 20)];

CAShapeLayer *maskLayer = [CAShapeLayer layer];

maskLayer.frame = view.bounds;

maskLayer.path = maskPath.CGPath;

view.layer.mask = maskLayer;

9.将一个view放置在其兄弟视图的最上面

[parentView bringSubviewToFront:yourView]

10.将一个view放置在其兄弟视图的最下面

[parentView sendSubviewToBack:yourView]

11.获取view的坐标在整个window上的位置

// v上的(0, 0)点在toView上的位置

CGPoint point = [v convertPoint:CGPointMake(0, 0) toView:[UIApplication sharedApplication].windows.lastObject];

或者

CGPoint point = [v.superview convertPoint:v.frame.origin toView:[UIApplication sharedApplication].windows.lastObject];

12.获取一个view所属的控制器

// view分类方法

- (UIViewController *)belongViewController {

for (UIView *next = [self superview]; next; next = next.superview) {

UIResponder* nextResponder = [next nextResponder];

if ([nextResponder isKindOfClass:[UIViewController class]]) {

return (UIViewController *)nextResponder;

}

}

return nil;

}

13.scrollView滚动到最下边

CGPoint bottomOffset = CGPointMake(0, scrollView.contentSize.height - scrollView.bounds.size.height);

[scrollView setContentOffset:bottomOffset animated:YES];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 4,819评论 1 6
  • 1、禁止手机睡眠 [UIApplicationsharedApplication].idleTimerDisabl...
    小热狗阅读 4,453评论 0 2
  • 最近项目需要,做一个钱包,里面有个类似支付宝账单的东西,界面的话就是自定义tableViewcell,数据的话,头...
    晨阳聊电影阅读 6,263评论 0 1
  • 捷径五:【交流】 交流是一件很愉快的事情,我觉得。 交流的对象在我看来可以用一种简单粗糙地方式分为两种:说得来的,...
    6da04c2370c7阅读 2,844评论 0 0
  • 毁掉一个人的方式,给他一份工资不会低的闲职,没有压力,工作环境很放松,没有考核指标,还包埋班车接送,工作时间短……...
    粥niki阅读 1,950评论 1 1

友情链接更多精彩内容