gpt4 book ai didi

ios - 如何以编程方式在 UIScrollView 中使用 AutoLayout 设置 subview ?

转载 作者:技术小花猫 更新时间:2023-10-29 10:44:57 25 4
gpt4 key购买 nike

我正在使用自动布局以编程方式使用 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];
}

现在我正在获取任何 subview ,

但是如果我改变方向它会给出不正确的结果,那么我如何为 ScrollView 的 subview 提供 NSLayoutConstraint

编辑

添加NSLayoutConstraint 后 subview 不显示。我只是缺少一些约束,请在动态设置约束方面纠正我。

我的 Source Code is here ,请帮助我。谢谢你。抱歉语法错误。

最佳答案

根据我使用 AutoLayout 的经验(我也以编程方式使用)。您不应将 View 直接添加到您的 UIScrollView

你应该做一个 UIView 而且只有一个,你添加为你的 UIScrollView 的 subview 。

完成此操作后,您可以将所有 subview 添加到此 UIView

此处详细解释了所有内容:Apple iOS Technical Note TN2154

关于ios - 如何以编程方式在 UIScrollView 中使用 AutoLayout 设置 subview ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21934558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com