問題描述
我正在使用自動布局以編程方式創建帶有 UiScrollview 和 UIPagectontrol 的應用程序,用于
I am creating app with UiScrollview and UIPagectontrol using Autolayout Programmatically, for
我已經創建了 TKScroller 作為 UIView 的子類,我使用 Some Mode 和 Array 對其進行初始化.
I have Created TKScroller as subclass of UIView, I am init it using Some Mode and Array.
TKScroller.m
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];
}
現在我正在獲取任何子視圖,
Right now i am getting any subviews,
但是如果我改變方向它會給出不正確的結果,那么我怎樣才能為滾動視圖的子視圖提供 NSLayoutConstraint
呢?
but if i change orientation it will give incorrect result, So how can i give NSLayoutConstraint
for subviews of scrollview ?
編輯
添加 NSLayoutConstraint
后,子視圖不顯示.我只是缺少一些約束,請在動態設置約束時糾正我.
After adding NSLayoutConstraint
subviews are not shown. I am just missing some constraint , please correct me in setting constraints dynamically.
我的源代碼在這里,請幫幫我.謝謝你.抱歉語法不好.
My Source Code is here , Please Help Me. Thank you. And Sorry for bad grammar.
推薦答案
Here is a SO answer 解釋了如何使用自動布局做到這一點,他已經完美地解釋了,這里有垂直文本字段,但在你的情況下,你必須設置水平視圖約束.
Here is a SO answer has explained how to do this with auto layout, he has explain perfectly , In here there is vertically textfields are there But in your case it is you have to set Horizontal views constraints.
替代方案
設置約束你可以只設置子視圖的框架并在滾動視圖中設置它,并且基于方向你可以改變滾動視圖的子視圖的框架.
Rather that setting constraints you can set just frame of the subview and set it in Scrollview, And based on orientation you can change frames of the scrolview's subviews.
你的 setData 方法比如,
Your setData Method like,
-(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
方法,
Now you using NSNotificationCenter you can get notify when device orientation chaged, so in this selector method of it call your setData
method,
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setData)
name:UIDeviceOrientationDidChangeNotification
object:nil];
現在在您的 setData 方法中,您需要刪除所有子視圖,因為當設備更改方向時,它將向您的滾動視圖添加新視圖,因此在設置其框架之前從 Scrollview 中刪除所有子視圖,
Now in you setData method you need remove all subviews because when device changes Orientation it will add new views to your scrollview, so remove all subview from Scrollview before setting its frame,
[scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
確保您正在從班級中移除觀察者,例如,
Make sure you are removing observer from your class like,
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
這篇關于如何以編程方式在 UIScrollView 中使用 AutoLayout 設置子視圖?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!