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.模拟器报错


iOS学习资料十一之View_第1张图片

解决办法:

打开模拟器->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];

你可能感兴趣的:(iOS学习资料十一之View)