Ios 如何在视图上重新显示视图?
我在Ios 如何在视图上重新显示视图?,ios,objective-c,uiscrollview,Ios,Objective C,Uiscrollview,我在视图中将出现以下代码: CGFloat typeViewHeight = self.scrollview.frame.size.height; CGFloat typeViewWidth = 120; for (UIView *view in [self.scrollview subviews]) { if([view isKindOfClass:[TypeSelectionItemView class]]) { [view removeFromSuperview]
视图中将出现以下代码:
CGFloat typeViewHeight = self.scrollview.frame.size.height;
CGFloat typeViewWidth = 120;
for (UIView *view in [self.scrollview subviews]) {
if([view isKindOfClass:[TypeSelectionItemView class]]) {
[view removeFromSuperview];
}
}
for (int i = 0; i < typeArray.count; ++i) {
TypeSelectionItemView *typeView = nil;
CGRect frame = CGRectMake(typeViewWidth * i, 0, typeViewWidth, typeViewHeight);
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TypeSelectionItemView" owner:self options:nil];
for(id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[TypeSelectionItemView class]]){
typeView = (TypeSelectionItemView *) currentObject;
break;
}
}
[self.typeScrollView setContentSize:CGSizeMake(typeViewWidth * (i + 1), typeViewHeight)];
[self.typeScrollView addSubview:typeView];
}
将视图中的代码移动到视图将布局子视图
如果您需要在viewdide出现之前获取任何视图的真实帧/大小
把这两行放在上面:
// Force scrollView to layout subview
scrollView.setNeedsLayout()
scrollView.layoutIfNeeded()
// ...
希望它有用 将代码放在-(void)viewWillLayoutSubviews
方法中,而不是viewwillbeen
中,然后它将被多次调用。仅在viewWillLayoutSubviews内调用一次help@Rendy将代码移动到视图willlayoutsubviews
,并使用布尔值检查并使其仅调用一次。请尝试@trungduc的建议。应该可以了是的,已经可以了。。早些时候,还有另一个问题,因此设法解决了这两个问题。。谢谢
// Force scrollView to layout subview
scrollView.setNeedsLayout()
scrollView.layoutIfNeeded()
// ...