ios 将条形按钮项添加到导航项的左侧

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22581458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:47:38  来源:igfitidea点击:

Adding Bar Button Item to Left of Navigation Item

iosxcodestoryboardxcode5xcode-storyboard

提问by LondonAppDev

I am following thistutorial on adding a sidebar navigation to an app.

我正在关注有关向应用程序添加侧边栏导航的教程。

In the given source code they have provided the storyboard layouts pre-defined. One of them is a view that has a "Navigation Item" and a "Bar Button Item" which appears on the left of the top navigation bar.

在给定的源代码中,他们提供了预定义的故事板布局。其中之一是具有“导航项”和“栏按钮项”的视图,该视图显示在顶部导航栏的左侧。

I am trying to do the same in my app, however the button item keeps automatically appearing on the right, and I cannot find any way to move it to the left.

我试图在我的应用程序中做同样的事情,但是按钮项目一直自动出现在右侧,我找不到任何方法将其移动到左侧。

I noticed, in the example provided in the tutorial, it gives this outlet option leftBarButton option:

我注意到,在教程中提供的示例中,它给出了这个插座选项 leftBarButton 选项:

enter image description here

在此处输入图片说明

And when I check mine, it looks like this:

当我检查我的时,它看起来像这样:

enter image description here

在此处输入图片说明

There is no navigation controller because of how SWRevealView works. When I run the app, I can see the button and it works perfectly, only issue is it's on the right hand side. I've compared my view with the example, and cannot seem to find any difference.

由于 SWRevealView 的工作方式,没有导航控制器。当我运行应用程序时,我可以看到按钮并且它运行良好,唯一的问题是它在右侧。我已经将我的观点与示例进行了比较,似乎找不到任何区别。

Any help is appreciated.

任何帮助表示赞赏。

EDITThis might help. In storyboards, this is what the working example looks like:

编辑这可能会有所帮助。在故事板中,这是工作示例的样子:

enter image description here

在此处输入图片说明

And this is what mine looks like

这就是我的样子

enter image description here

在此处输入图片说明

回答by Amit

Drag BarButtonItem and place left side on your navigation item.

拖动 BarButtonItem 并将左侧放在导航项上。

回答by Chesh

I could not find a way to manage it in storyboard.So i coded it in my viewcontroller's ViewDidLoad function adding the following code

我找不到在故事板中管理它的方法。所以我在我的视图控制器的 ViewDidLoad 函数中编码它添加以下代码

self.navigationItem.leftBarButtonItem = _sidebarButton;
self.navigationItem.rightBarButtonItem = nil;

回答by SLyHuy

1/. Right click on Storyboard > Open as > Source Code

1/。右键单击Storyboard > Open as > Source Code

2/. Find your View Controller by Ctrl + F : <name of View Controller>

2/。通过以下方式找到您的视图控制器Ctrl + F : <name of View Controller>

3/. Find navigationItem> You will see barButtonItemlike this:

3/。查找navigationItem> 你会看到barButtonItem这样的:

<navigationItem key="navigationItem" title="Title" id="n9s-Cu-Dtc">
   <barButtonItem key="rightBarButtonItem" title="Item" image="menu-icon-2.png" id="aN6-4T-o4T"/>
</navigationItem>

4/. Change key="rightBarButtonItem"to key="leftBarButtonItem"

4/。更改key="rightBarButtonItem"key="leftBarButtonItem"

Save and finish. Right click on Storyboard > Open as > Builder Interface

保存并完成。右键单击Storyboard > Open as > Builder Interface

回答by sirhcmcd

I had the exact same issue. It visually appeared that I didn't have a navigation bar on my main view controller, but it was there.

我有完全相同的问题。从视觉上看,我的主视图控制器上没有导航栏,但它在那里。

I eventually discovered that if I clicked on the navigation view controller first, the navigation bar on the main view controller appeared. I placed a button bar item on the nav bar, but then deleted it (just needed it temporarily to enable images in the nav bar). I was then able to drag the menu image I created from my Images.xcassets onto the Main view controller's nav bar to create the correct button bar item. You just need to select the Media library icon in the right hand Utilities section to find the images and use them. Then just set the sidebarButton Referencing Outlet in the associated myviewcontroller.h file. You can do this quickly by selecting the menu image on their nav bars while holding your control key, then dragging it onto the sidebarButton property in the associated viewcontroller.h file with the assistant editor split screen mode.

我最终发现,如果我先点击导航视图控制器,主视图控制器上的导航栏就会出现。我在导航栏上放置了一个按钮栏项目,但随后将其删除(只是暂时需要它以在导航栏中启用图像)。然后我能够将我从我的 Images.xcassets 创建的菜单图像拖到主视图控制器的导航栏上以创建正确的按钮栏项目。您只需选择右侧“实用工具”部分中的媒体库图标即可找到图像并使用它们。然后只需在关联的 myviewcontroller.h 文件中设置 sidebarButton 引用出口。您可以通过在按住控制键的同时选择导航栏上的菜单图像,然后将其拖到相关视图控制器中的 sidebarButton 属性上来快速完成此操作。

On the other view controllers in my app (similar to the Photo and Map view controllers), I just had to make the view controllers have the Translucent Navigation Bar setting for their Top Bar in the view controllers attributes section (in the storyboard Utilities). The identical nav bars from the main view controller then appeared on them. I also had to make their similarly create the menu image button bar items and set the sidebarButton Referencing Outlet in their *.h file. Yu can do this quickly by selecting the menu image on their nav bars, then selecting the referencing outlets (+) sign and dragging it onto the view controller's yellow circle icon while holding the ctrl key.

在我的应用程序中的其他视图控制器(类似于照片和地图视图控制器)上,我只需要让视图控制器在视图控制器属性部分(在故事板实用程序中)为其顶部栏设置半透明导航栏。来自主视图控制器的相同导航栏然后出现在它们上面。我还必须让他们类似地创建菜单图像按钮栏项目,并在他们的 *.h 文件中设置 sidebarButton 引用出口。Yu 可以通过选择导航栏上的菜单图像快速完成此操作,然后选择引用出口 (+) 符号并将其拖到视图控制器的黄色圆圈图标上,同时按住 ctrl 键。

Not sure if I missed something in the instructions, but this worked for me.

不确定我是否遗漏了说明中的某些内容,但这对我有用。

回答by David Douglas

You can also add left hand side buttons by code:

您还可以通过代码添加左侧按钮:

NSMutableArray *leftBtns = [[NSMutableArray alloc] init];

UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"LeftButton"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonPressed)];
[leftBtns addObject:leftBtn];

[self.navigationItem setLeftBarButtonItems:leftBtns animated:NO];

// method

// 方法

-(void)leftButtonPressed
{
    NSLog(@"Left Button Tapped");
}

回答by saraman

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];

    [button setImage:[[UIImage imageNamed:@"BtnBack"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [button.imageView setTintColor:[UIColor whiteColor]];
    [button addTarget:self action:@selector(buttonClicked:)
     forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
                                   initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = buttonItem;

回答by user3354984

I solved it by dragging navigation bar from vc1 to vc2 in the document outline. The navigation bar is now available in scoreboard for dropping navigation barbuttonitems.

我通过在文档大纲中将导航栏从 vc1 拖动到 vc2 来解决它。导航栏现在在记分板中可用,用于放置导航栏按钮项目。