• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何通过工具栏按钮替换容器 View ?

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:08:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我想创建一个用户界面,其中有一个导航栏及其按钮,下方有一个工具栏,两者始终可见。然后,我希望底部有一个工具栏,当您点击按钮时,它将替换屏幕中间可见的内容。我会使用 UITabBarController 但我不想要下面的文本标签或图标的图像 - 按钮将是一个 unicode 字符。它们应该是灰色的,然后在选择一个时用浅色填充。我在想这样做的方法是在点击按钮时应用色调,但由于条形按钮没有粘性“选定状态”,我不知道该怎么做。

我也不确定如何替换中间的内容。我在想我应该创建一个容器,然后从每个按钮创建一个操作方法,该方法将 performSegueWithIdentifier 到一个包含不同内容的不同容器。但是只能嵌入一个 View Controller ——我不能从容器中创建多个 segue。我基本上是在尝试用较小的按钮模拟一个标签栏 Controller ,没有标签,并且屏幕上的整个内容不会在点击按钮时被替换。

我该怎么做呢?我想在 Interface Builder 中完成基本布局,但当然需要一些代码。谢谢!

这基本上是我想要实现的目标: enter image description here



Best Answer-推荐答案


这是我过去的做法。代码位于自定义容器 Controller 中,该 Controller 是嵌入在 Storyboard的容器 View 中的 Controller 。因此,在 viewDidAppear: 中,我将第一个 Controller 添加为子级,并在单击按钮时调用 switchToNewView:。

@implementation ContainerController


- (void)viewDidAppearBOOL)animated {
    [super viewDidAppear:animated];
    UIViewController *initial = [self.storyboard instantiateViewControllerWithIdentifier"InitialVC"];
    [self addChildViewController:initial];
    [self.view addSubview:initial.view];
    self.currentController = initial;
    [self constrainViewEqual:self.currentController];
}

-(IBAction)switchToNewView {
    UIViewController *sub = [self.storyboard instantiateViewControllerWithIdentifier"SubstituteVC"];
    [self addChildViewController:sub];
    sub.view.frame = self.view.bounds;
    [self moveToNewController:sub];
}

-(void)moveToNewControllerUIViewController *) newController {
    [self.currentController willMoveToParentViewController:nil];
    [self transitionFromViewController:self.currentController toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{}
         completion:^(BOOL finished) {
             [self.currentController removeFromParentViewController];
             [newController didMoveToParentViewController:self];
             self.currentController = newController;
             [self constrainViewEqual:self.currentController];
         }];
}

constrainViewEqual 是我为 Controller View 添加约束的类别方法。看起来是这样的,

-(void)constrainViewEqualUIViewController *) vc {
    [vc.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    NSLayoutConstraint *con1 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterX relatedBy:0 toItem:vc.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    NSLayoutConstraint *con2 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:vc.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
    NSLayoutConstraint *con3 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeWidth relatedBy:0 toItem:vc.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    NSLayoutConstraint *con4 = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:0 toItem:vc.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    NSArray *constraints = @[con1,con2,con3,con4];
    [self.view addConstraints:constraints];
}

关于ios - 如何通过工具栏按钮替换容器 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22869856/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap