如何在Swift 3中创建像Reddit App这样的顶级导航

时间:2018-07-03 11:21:54

标签: swift3 uisegmentedcontrol

这是我要创建的UI

The UI I want to create

以及完整的UI外观:

Complete UI

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

1 个答案:

答案 0 :(得分:0)

好的。这是解决方案。

第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在情节提要中可能看起来像这样。

Consider This

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

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