本文介绍了如何在Swift 3中创建像Reddit App这样的顶级导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要创建的UI:

以及完整的UI外观:

但是我想创建第一个顶级导航.我尝试了 https://github.com/subinspathilettu/SJSegmentedViewController 正在运行,但显示导航后显示SegmentView.有人可以帮助我创建此UI,就像登录后的reddit应用程序屏幕一样.我需要迅速使用.在此先感谢

but i want to create first top Navigation. i tried https://github.com/subinspathilettu/SJSegmentedViewControllerWhich is working but it shows navigation after that it shows SegmentView.Could someone help me to create this UI it's same like reddit app screen after login.I need in swift.Thanks in advance

推荐答案

好的.这是解决方案.

Okay. Here is the solution.

第1步:

  1. 不行. UIView中需要的按钮的数量.
  2. 将UILabel设置为每个按钮的宽度相等,并将高度设置为1.0,并将其放置在每个按钮下方.
  3. 现在将Outlet分配给ViewController中的每个按钮及其受尊敬的标签.
  4. 现在在此UIView下获取UIScrollView并设置约束并提供出口.
  5. 现在,您必须为导航"中所需的每个选项卡创建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在情节提要中可能看起来像这样.

Your ViewController may look like this in your Storyboard.

我希望这会帮助您或至少给您一些想法,以开发您所需的东西.对不起,我在Objective C和一些凌乱的代码中的回答.其实我很着急.但这肯定会对您有所帮助.谢谢! :)

I hope this will help you or at least give you some idea to develop what you need. Sorry for my answer in Objective C and some messy code. Actually I am in a hurry. But this will help you for sure. Thanks! :)

注意:- 请严格按照以下步骤操作.您肯定会找到解决方案.谢谢. :)

这篇关于如何在Swift 3中创建像Reddit App这样的顶级导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:21