【问题标题】:How to create top Navigation like Reddit App in Swift 3如何在 Swift 3 中创建类似于 Reddit 应用的顶级导航
【发布时间】:2018-12-11 16:41:58
【问题描述】:

这是我要创建的UI

以及完整的用户界面是什么样的:

但我想创建第一个顶级导航。我试过https://github.com/subinspathilettu/SJSegmentedViewController 哪个有效,但在显示 SegmentView 之后显示导航。有人可以帮我创建这个 UI,它就像登录后的 reddit 应用程序屏幕一样。我需要 swift。提前致谢

【问题讨论】:

  • 如果您可以使用第三方库,那么我想github.com/rechsteiner/Parchment 最适合您
  • @SureshVarma 兄弟,你能告诉我吗,我不知道该怎么做,我想要那些笑话和模因在导航的顶部
  • @Michael Dautermann 帮我解决这个问题,请提供答案
  • 我已经完成了这个类型,花几秒钟,删除图像视图并添加按钮stackoverflow.com/questions/51011242/…
  • @chandra1234 我将如何使用像 pageMenu 这样的滑动操作,你能回答我的问题吗

标签: swift3 uisegmentedcontrol


【解决方案1】:

好的。这是解决方案。

第 1 步:

  1. 拿不。 UIView 中所需的按钮数量。
  2. 将 UILabel 取为每个按钮的宽度相等,高度应为 1.0,并将其放在每个按钮的下方。
  3. 现在为您的 ViewController 中的每个按钮及其相关标签提供 Outlets。
  4. 现在在这个 UIView 下取 UIScrollView 并设置约束并给出出口。
  5. 现在您必须在 Too Navigation 中为所需的每个选项卡创建 ViewController。

例如:-

  • 我有一个 ProfileViewController.m,如下所示。

    @interface ProfileViewController ()<UIScrollViewDelegate>

        @property (nonatomic,strong) AboutMeViewController *aboutMeVC; 
        @property (nonatomic,strong) AddressViewController *addressVC;

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.


        _btnAboutMe.selected = NO;
        _btnAddress.selected = YES;
        _btnPassword.selected = NO;

        [self topButtonTapped:_btnAddress]; // This will call the navigation button tap method.


        _controllerArray = [@[self.aboutMeVC,self.addressVC,[self.storyboard instantiateViewControllerWithIdentifier:@"changePasswordVC"]] mutableCopy];
        [self addChildViewControllersOntoContainer:_controllerArray];

        //self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self  action:@selector(presentLeftMenuViewController:)];

        [_leftBarButton setTarget:self];
        [_leftBarButton setAction:@selector(presentLeftMenuViewController:)];

        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(profileTap:)];
        tapGesture.numberOfTapsRequired = 1;
        [_profilePicture addGestureRecognizer:tapGesture];

    }

    -(AboutMeViewController*)aboutMeVC
    {
        if (!_aboutMeVC) {
            _aboutMeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"aboutMeVC"];
        }
        _aboutMeVC.delegate = self;
        return _aboutMeVC;
    }

    -(AddressViewController*)addressVC
    {
        if (!_addressVC) {
            _addressVC = [self.storyboard instantiateViewControllerWithIdentifier:@"addressVC"];
        }
        return _addressVC;
    }

    // Adding all related controllers into the container
    -(void) addChildViewControllersOntoContainer:(NSArray *)controllersArr
    {
        for (int i = 0; i < controllersArr.count; i++)
        {
            UIViewController *vc = (UIViewController *)[controllersArr objectAtIndex:i];
            CGRect frame = CGRectMake(0, 0, self.containerScrollView.frame.size.width, self.containerScrollView.frame.size.height);
            frame.origin.x = SCREEN_WIDTH * i;
            vc.view.frame = frame;

            [self addChildViewController:vc];
            [self.containerScrollView addSubview:vc.view];
            [vc didMoveToParentViewController:self];
        }

        self.containerScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * controllersArr.count + 1, self.containerScrollView.frame.size.height - 99);
        self.containerScrollView.pagingEnabled = YES;
        self.containerScrollView.delegate = self;
    }

    // Scroll view delegate methods. 
   // This method will help you to change the navigation by sliding it.
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        NSInteger page = (scrollView.contentOffset.x / SCREEN_WIDTH);
        [self changeButtonState:page];
        //_selectedMenu = page;
        //_segmentControl.selectedSegmentIndex = page;
        NSLog(@"%@", NSStringFromCGSize(scrollView.contentSize));

        //    float xx = scrollView.contentOffset.x * (buttonWidth / SCREEN_WIDTH) - buttonWidth;
        //    [self.menuScrollView scrollRectToVisible:CGRectMake(xx, 0, SCREEN_WIDTH, self.menuScrollView.frame.size.height) animated:YES];
    }

    - (IBAction)topButtonTapped:(UIButton *)sender // Here is the method when you click on top button of your navigation bar.
    {
        [self changeButtonState:sender.tag];
        [self.containerScrollView setContentOffset:CGPointMake(SCREEN_WIDTH * sender.tag, 0) animated:YES];
    }

    -(void)changeButtonState:(NSInteger)tag
    {
        if (tag == 0)
        {
            _btnAboutMe.selected = YES;
            _btnAddress.selected = NO;
            _btnPassword.selected = NO;

            _btnAboutMe.fillColor = [GeneralClass getColorFromHex:kBlueColor withAlpho:1.0];
            _btnAddress.fillColor = [GeneralClass getColorFromHex:kWhiteColor withAlpho:1.0];
            _btnPassword.fillColor = [GeneralClass getColorFromHex:kWhiteColor withAlpho:1.0];

        }
        else if (tag == 1)
        {
            _btnAboutMe.selected = NO;
            _btnAddress.selected = YES;
            _btnPassword.selected = NO;

            _btnAboutMe.fillColor = [GeneralClass getColorFromHex:kWhiteColor withAlpho:1.0];
            _btnAddress.fillColor = [GeneralClass getColorFromHex:kBlueColor withAlpho:1.0];
            _btnPassword.fillColor = [GeneralClass getColorFromHex:kWhiteColor withAlpho:1.0];
        }
        else if (tag == 2)
        {
            _btnAboutMe.selected = NO;
            _btnAddress.selected = NO;
            _btnPassword.selected = YES;

            _btnAboutMe.fillColor = [GeneralClass getColorFromHex:kWhiteColor withAlpho:1.0];
            _btnAddress.fillColor = [GeneralClass getColorFromHex:kWhiteColor withAlpho:1.0];
            _btnPassword.fillColor = [GeneralClass getColorFromHex:kBlueColor withAlpho:1.0];
        }
    }

您的 ViewController 在 Storyboard 中可能如下所示。

我希望这将帮助您,或者至少给您一些想法来开发您需要的东西。很抱歉我在 Objective C 中的回答和一些混乱的代码。其实我很着急。但这肯定会对您有所帮助。谢谢! :)

注意:- 请严格按照步骤操作。你肯定会找到你的解决方案。谢谢。 :)

【讨论】:

  • 你能分享示例链接或你的项目吗?这样很容易理解这个概念
猜你喜欢
  • 2016-10-15
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多