如何在iOS中为HMSegmentControl加载UIViews而不是UILabels

阿南德·高塔姆(Anand Gautam)

我正在使用iPhone应用程序,需要在其中使用多个标题添加细分。对于解决方案,我在我的应用程序中添加了HMSegmentControl库。它工作正常,但问题是使用UILabels可以正常工作。相反,当用户点击任何细分标题按钮时,我需要加载UIView来显示屏幕。我已经尝试过了,但是没有用。基本上,我想为各个段加载UIViews(在XIB中连接)。

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

     screenBounds = [[UIScreen mainScreen] bounds];

    scrollTitle = [[NSMutableArray alloc]initWithObjects:@"Alerts",@"Profile",@"Help",nil];
    tabBarArray = [[NSMutableArray alloc]initWithObjects:@"Alerts",@"Profile",@"Help",nil];

    // Do any additional setup after loading the view from its nib.
    currentPage = 0;

    segmentedControll = [[HMSegmentedControl alloc] initWithSectionTitles:@[@"Alerts",@"Profile",@"Help"]];
    segmentedControll.backgroundColor = [UIColor colorWithRed:227/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
    [segmentedControll setFrame:CGRectMake(0, 46, 320, 40)];
    [segmentedControll setSelectedSegmentIndex:1];
    containerView = [[HMSegmentedControlContainerView alloc] initWithHMSegmentedControl:segmentedControll andMinimumWIdth:107];

    containerView.scrollView.showsHorizontalScrollIndicator=0;

    __weak typeof(self) weakSelf = self;
    [segmentedControll setIndexChangeBlock:^(NSInteger index) {
        [weakSelf.menuScrollView scrollRectToVisible:CGRectMake(320 * index, 0, 320, 200) animated:YES];
    }];

    [self.view addSubview:containerView];
    totalNoOfSegment=3;
    currentPage=segmentedControll.selectedSegmentIndex;

    self.menuScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 87, 320, 373)];
    [self.menuScrollView setBackgroundColor:[UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1]];
    [self.menuScrollView setPagingEnabled:YES];
    [self.menuScrollView setShowsHorizontalScrollIndicator:NO];
    [self.menuScrollView setContentSize:CGSizeMake(960, 200)];
    [self.menuScrollView scrollRectToVisible:CGRectMake(320, 0, 320, 200) animated:NO];
    [self.menuScrollView setDelegate:self];
    [self.view addSubview:self.menuScrollView];

    /*UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 373)];
    [label1 setText:@"Alerts"];
    label1.textColor = [UIColor blackColor];
    label1.backgroundColor = [UIColor whiteColor];
    label1.textAlignment = NSTextAlignmentCenter;
    [self.menuScrollView addSubview:label1];*/

    [self.menuScrollView addSubview:viewAlerts];

    /*UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(320, 0, 320, 373)];
    [label2 setText:@"Profile"];
    label2.textColor = [UIColor blackColor];
    label2.backgroundColor = [UIColor whiteColor];
    label2.textAlignment = NSTextAlignmentCenter;
    [self.menuScrollView addSubview:label2];*/

    [self.menuScrollView addSubview:viewProfile];

    /*UILabel *label3 = [[UILabel alloc] initWithFrame:CGRectMake(640, 0, 320, 373)];
    [label3 setText:@"Help"];
    label3.textColor = [UIColor blackColor];
    label3.backgroundColor = [UIColor whiteColor];
    label3.textAlignment = NSTextAlignmentCenter;
    [self.menuScrollView addSubview:label3];*/

    [self.menuScrollView addSubview:viewHelp];


    self.panGestureLeft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(panTableView:)];
    self.panGestureLeft.direction=UISwipeGestureRecognizerDirectionLeft ;
    [self.menuScrollView addGestureRecognizer:self.panGestureLeft];
    self.panGestureRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(panTableView:)];
    self.panGestureRight.direction=UISwipeGestureRecognizerDirectionRight ;
    [self.menuScrollView addGestureRecognizer:self.panGestureRight];
}

-(void)panTableView:(UISwipeGestureRecognizer *)pan{
    if (pan.state==UIGestureRecognizerStateEnded) {
        int curr=currentPage;

        if (pan==self.panGestureLeft)
        {
            // user dragged towards the right
            curr=curr+1;
            if (curr!=3) {
                CATransition *transition = [CATransition animation];
                transition.duration = 0.35;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.type = kCATransitionPush;
                transition.subtype =kCATransitionFromRight;
                transition.delegate = self;
                [self.menuScrollView.layer addAnimation:transition forKey:nil];
                segmentedControll.selectedSegmentIndex=curr;
                [self segmentedControlChangedValue:segmentedControll];
            }

        }
        else if (pan==self.panGestureRight)

        {
            // user dragged towards the left
            curr=curr-1;
            if (curr!=-1) {
                CATransition *transition = [CATransition animation];
                transition.duration = 0.35;
                transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
                transition.type = kCATransitionPush;
                transition.subtype =kCATransitionFromLeft;
                transition.delegate = self;
                [self.menuScrollView.layer addAnimation:transition forKey:nil];
                segmentedControll.selectedSegmentIndex=curr;
                [self segmentedControlChangedValue:segmentedControll];

            }
        }
    }
}

- (void)segmentedControlChangedValue:(HMSegmentedControl *)segmentedControl {
    NSLog(@"Selected index %i (via UIControlEventValueChanged)", segmentedControl.selectedSegmentIndex);
    currentPage=segmentedControl.selectedSegmentIndex;
}

在我的代码中,如果我将动态添加UIViews,那么它将起作用。但是我需要使用XIB添加UIViews。请帮助我实现解决方案。谢谢

吉特

您错过的代码几乎是唯一正确的事情是UIViews的框架,因为您是将它们加载到以编程方式制作的ScrollView中,因此您需要使用ScrollView设置UIviews的框架,同时考虑ScrollView的内容偏移量

for (int i = 0; i < numberOfView; i++) {


    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

//现在将此框架相应地设置为您的视图

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在iOS中屏蔽UIViews

如何从Multipeer Connectivity Session中更改视图UILabels,UIButtons,UIViews等?

如何在iOS中从NIB加载NIB

如何在 UITableViewCell 中向 UIViews 添加重力?

如何在Webpack中为特定路径配置加载程序

如何在角度4中为特定路线加载脚本

如何在Chrome中将“新标签”页面加载为“ Google”而不是“ Google Canada”?

如何在iOS中将FilePath加载到UIWebbview中?

如何在iOS Swift中制作加载动画?

如何在iOS 9中加载HTTPS URL?

如何在页面加载时将我的默认选中复选框设置为复选框列表中的最新日期,而不是全选?

在情节提要中为UIButton交换UIViews

如何在 Xamarin iOS 中为导航栏设置 ShadowImage

如何在iOS 8.3中检测设备是否为iPad?

如何在iOS 7中为按钮添加约束?

如何在iOS Swift中为CMSampleBufferGetFormatDescription创建anAudioSampleBuffer

如何在iOS中以编程方式为UICollectionView添加HeaderView

如何在iOS中为TableView单元设置角半径

如何在iOS中为暗模式更改设置动画?

如何在iOS中为UITextView设置内容插图

如何在iOS8中为UISplitViewcontroller隐藏masterView

如何在Swift中解析为TextField iOS钥匙串?

如何在Twitter中为iOS上载动画GIF?

如何在PhoneGap中为iOS设置启动屏幕

如何在Swift中为iOS创建SDK?

如何在iOS eventKit中为事件添加警报?

如何在iOS XCode中为JSON定义结构?

如何在iOS中为AvcaptureSession设置videoMaximumDuration

如何在Unity中为iOS设备应用界限