【问题标题】:How to load UIViews instead of UILabels for HMSegmentControl in iOS如何在 iOS 中为 HMSegmentControl 加载 UIViews 而不是 UILabels
【发布时间】:2014-07-25 17:27:14
【问题描述】:

我正在开发 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;
}

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

【问题讨论】:

  • 你的 self.menuScrollView 是在你的 Xib 中还是你以编程方式创建的
  • 感谢@Geet,我以编程方式创建了 self.menuScrollView。
  • 您是否将您的 UIView 与界面中的属性关联起来?
  • 那么,这可能有点困难,你可以做的是在你的 XIB 中创建 uIVIew 作为主视图的子视图,首先将它们隐藏,然后将它们作为子视图添加到ScrollView 通过设置它的 contentOffset,并取消隐藏它们,它可能会起作用
  • 在我上面的代码中.. 我只加载 UILabels。我尝试使用 XIB 将 UIViews 添加到滚动视图,但它不起作用。

标签: ios iphone uiview uisegmentedcontrol


【解决方案1】:

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

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;

//现在将这个Frame设置为你的Views

}

【讨论】:

  • 非常感谢.. @Geet 无话可说。
  • 我建议使用 CGGeometry 函数,例如 CGRectGetWidth(CGRect rect) 来检索框架元素,而不是直接访问宽度成员。 ObjC 写作风格的更多细节和其他有用提示:github.com/macoscope/objc-style-guide
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多