我需要我的这个问题,我需要工作UIView
为ScrollView
一些严重的帮助。在上传的图片下面会清除所有的疑惑。如何使UIView的工作作为一个滚动视图
我已经采取了四个按钮与下面他们UIView
与我已经把另一个之上的4点容器意见一起,以便随着四个按钮工作..我现在想要的是工作UIView
作为ScrollView
。每当我点击任何按钮,视图就会向前移动,并且它会更改容器视图。每当我点击一个按钮时,相应的容器视图就会显示
我需要我的这个问题,我需要工作UIView
为ScrollView
一些严重的帮助。在上传的图片下面会清除所有的疑惑。如何使UIView的工作作为一个滚动视图
我已经采取了四个按钮与下面他们UIView
与我已经把另一个之上的4点容器意见一起,以便随着四个按钮工作..我现在想要的是工作UIView
作为ScrollView
。每当我点击任何按钮,视图就会向前移动,并且它会更改容器视图。每当我点击一个按钮时,相应的容器视图就会显示
你好你可以更好的使用UIScrollView的,而不是像的UIView:
//Declare it
NSMutableArray * buttonArray
int selectedIndex;
-(void)setUpSegmentBar {
NSArray * namesOfMenus [email protected][@"Button1",@"Button2",@"Button3",@"Button4",@"Button5"];
// Scrollview added on storyBoard
myScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 2, self.view.frame.size.width, 40)];
CGFloat scrollWidth = 0.f;
buttonArray=[[NSMutableArray alloc]init];
for (int j=0; j<namesOfMenus.count; j++)
{
NSString * name =[namesOfMenus objectAtIndex:j];
CGSize size = [name sizeWithAttributes:
@{NSFontAttributeName: [UIFont fontWithName:font_bold size:font_size_button]}];
CGSize textSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
CGFloat strikeWidth;
if (iPAD) {
strikeWidth = self.view.frame.size.width/5.5;
}else{
strikeWidth = textSize.width;
}
CGRect frame = CGRectMake(scrollWidth, 0,strikeWidth+20, 40);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:j];
button.frame = frame;
[button setBackgroundColor:[UIColor whiteColor]];
button.titleLabel.textColor=[UIColor whiteColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.layer.borderColor=[UIColor whiteColor].CGColor;
button.titleLabel.textAlignment=NSTextAlignmentCenter;
[button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:name forState:UIControlStateNormal];
// ScrollWidth As per Button Width
scrollWidth= scrollWidth+strikeWidth+20;
// to Check Which Button Is Selected.
if (j==selectedIndex) {
// If Selected Do UI Changes
button.backgroundColor=segment_selected_Color ;
[button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
[button addShaddow];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}else {
// Other Buttons
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
button.backgroundColor=segment_disselected_Color;
[button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else{
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}
// Add Buttons in Array
[buttonArray addObject:button];
// Add button on Scroll View
[myScrollView addSubview:button];
}
myScrollView.contentSize = CGSizeMake(scrollWidth, 30.f);
myScrollView.pagingEnabled = NO;
myScrollView.backgroundColor=[UIColor whiteColor];
[myScrollView setShowsHorizontalScrollIndicator:NO];
[myScrollView setShowsVerticalScrollIndicator:NO];
return myScrollView;
}
和HANDELING按钮操作。
-(void)buttonEvent:(UIButton*)sender
{
NSInteger index= sender.tag;
selectedIndex= (int) index;
for(int i=0;i<buttonArray.count;i++)
{
UIButton * button =(UIButton*)[buttonArray objectAtIndex:i];
if (i==selectedIndex) {
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
button.backgroundColor=segment_selected_Color ;
[button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
[button addShaddow];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}else {
button.backgroundColor=segment_disselected_Color;
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
[button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else{
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}
}
CGRect frame1 = myScrollView.frame;
UIButton * bt=(UIButton*)[buttonArray objectAtIndex:index];
frame1 =bt.frame ;
// By using it button will scroll and show properly
[myScrollView scrollRectToVisible:frame1 animated:YES];
[self setupTableAfterClick];
}
通过使用你的代码我如何改变ContainerView? – pri
是的,你必须使用滚动视图作为基本视图,然后bycalling setupSegment你滚动视图添加按钮。如果你想对cantainer进行定制,那么在滚动视图上执行并且想要在定制按钮上进行定制。 –
@pri如果它能解决您的问题,请做出正确的决定。 –
可以显示乌尔试图代码 –
- (IBAction为)登录:(ID)发送方{ self.title = @ “LOGIN”; [_ScrollView setContentOffset:CGPointMake(0,0)的动画:YES]; } - (IBAction为)寄存器:(ID)发送方{ self.title = @ “REGISTER”; [_ScrollView setContentOffset:CGPointMake(_login.frame.size.width,0)动画:YES]; } 此代码是从我的其他项目......在这个项目我已经使用2个容器连同滚动视图及以上,与一个UIView 2buttons一起。我试图在这里应用相同的逻辑。但它没有发生 – pri
???????????????? – pri