簡體   English   中英

如何在iOS中為HMSegmentControl加載UIViews而不是UILabels

[英]How to load UIViews instead of UILabels for HMSegmentControl in iOS

我正在使用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;

//現在將此框架相應地設置為您的視圖

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM