【发布时间】:2014-03-22 23:24:14
【问题描述】:
我正在使用自动布局以编程方式创建带有 UiScrollview 和 UIPagectontrol 的应用程序,用于
我已经创建了 TKScroller 作为 UIView 的子类,我使用 Some Mode 和 Array 对其进行初始化。
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?
编辑
添加NSLayoutConstraint 后不显示子视图。我只是缺少一些约束,请在动态设置约束时纠正我。
我的Source Code is here,请帮帮我。 谢谢你。抱歉语法不好。
【问题讨论】:
-
“不正确的结果”是什么意思?
-
@AncAinu:pageControl 消失了。
-
首先,如果您以编程方式执行
AutoLayout,我建议您使用use this great Framework,这将使您的生活更加轻松,您的代码也更加可读;) -
@AncAinu:感谢您的建议,我不想为此使用其他一些课程。请问你能帮我目前的工具,我只是缺少一些东西。
-
旋转设备时,您是否看到调试控制台中显示任何信息?例如。无法同时满足约束。通常调试控制台会给你一些关于为什么视图没有正确显示的提示。此外,您应该在子视图上设置 translatesAutoresizingMaskIntoConstraints = NO。
标签: ios iphone uiscrollview autolayout nslayoutconstraint