我正在使用Autolayout Progrmatically创建UiScrollview和UIPagectontrol的应用程序
我创建了TKScroller作为UIView的子类,我使用一些模式和数组来初始化它.
TKScroller.m
-(void)setData{ [self layoutIfNeeded]; CGRect mainFrame=self.frame; [self layoutIfNeeded]; CGRect mainFrame=self.frame; UIView *lastview; NSMutableArray* manualConstraints = [NSMutableArray array]; for (int i=0; i<arrayData.count ; i++) { CGRect frame; frame.origin.x = scrollView.frame.size.width * i; frame.origin.y = 0; frame.size = scrollView.frame.size; UIView *subview = [UIView new]; subview.backgroundColor = [self getRandomColor]; [scrollView addSubview:subview]; if (i==0) { NSLayoutConstraint* b1_top = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1 constant:5]; [manualConstraints addObject:b1_top]; NSLayoutConstraint* b1_left = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1 constant:5]; [manualConstraints addObject:b1_left]; NSLayoutConstraint* b1_right = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-5]; [manualConstraints addObject:b1_right]; NSLayoutConstraint* b1_bottom = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1 constant:-10]; [manualConstraints addObject:b1_bottom]; [subview layoutIfNeeded]; [scrollView addConstraints:manualConstraints]; lastview=subview; } else{ NSLayoutConstraint* b1_top = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1 constant:5]; [manualConstraints addObject:b1_top]; NSLayoutConstraint* b1_left = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:lastview attribute:NSLayoutAttributeLeading multiplier:1 constant:5]; [manualConstraints addObject:b1_left]; NSLayoutConstraint* b1_right = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1 constant:-5]; [manualConstraints addObject:b1_right]; NSLayoutConstraint* b1_bottom = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1 constant:-10]; [manualConstraints addObject:b1_bottom]; [subview layoutIfNeeded]; [scrollView addConstraints:manualConstraints]; lastview=subview; } } scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * arrayData.count,mainFrame.size.height/2); self.pageControl.currentPage = 0; self.pageControl.numberOfPages = arrayData.count; pageControlBeingUsed = NO; } -(UIColor *)getRandomColor{ int r = arc4random() % 255; int g = arc4random() % 255; int b = arc4random() % 255; return [UIColor colorWithRed:(r/255.0) green:(g/255.0) blue:(b/255.0) alpha:1.0]; }
现在我得到任何子视图,
但是如果我改变方向会给出不正确的结果,那么我如何给NSLayoutConstraint的scrollview子视图?
编辑
添加NSLayoutConstraint子视图后不会显示.我只是错过了一些约束,请在动态设置约束时纠正我.
我的Source Code is here,请帮我
谢谢.对不起,糟糕的语法.
解决方法
@L_502_1@已经解释了如何做到这一点与汽车布局,他已经解释完美,在这里有垂直的textfields在那里但在你的情况下,你必须设置水平视图约束.
替代
而是设置约束,您可以设置子视图的框架,并将其设置在ScrollView中,并且根据方向,您可以更改scrolview的子视图的框架.
你的setData方法就像,
-(void)setData{ [self layoutIfNeeded]; CGRect mainFrame=scrollView.frame; CGRect frame; for (int i=0; i<arrayData.count ; i++) { CGRect frame; frame.origin.x = scrollView.frame.size.width * i; frame.origin.y = 0; frame.size = scrollView.frame.size; frame.origin=CGPointMake(0,0); UIView *subview = [[UIView alloc]initWithFrame:frame]; subview.backgroundColor = [self getRandomColor]; [scrollView addSubview:subview]; } scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * arrayData.count,mainFrame.size.height/2); }
现在你使用NSNotificationCenter可以在设备方向被通知时得到通知,所以在这种选择器方法中,它调用你的setData方法,
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setData) name:UIDeviceOrientationDidChangeNotification object:nil];
现在在您的setData方法中,您需要删除所有子视图,因为当设备更改Orientation时,它将向scrollview添加新视图,因此在设置其框架之前,从Scrollview中删除所有子视图,
[scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
确保你从你的类中删除观察者,
- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }