简体   繁体   English

自定义标签栏Swift

[英]Custom Tab Bar Swift

Hi I have searched all over and I still cannot find any information that would help me with this task. 嗨,我已经搜遍了所有,我仍然找不到任何可以帮助我完成这项任务的信息。 How would I create a custom tab bar that looks like this using Swift: 如何使用Swift创建一个看起来像这样的自定义标签栏:

Here is a link to the image of what the tab bar looks like: 以下是标签栏外观图像的链接: http://2.bp.blogspot.com/-QlGT8CjZqJw/VbUZDwlRXzI/AAAAAAAAEHg/zqT_1Valsvo/s1600/Tab%2Bbar.png

The tab bar would look like this and the selected tab would look like the middle icon, I have all the assets for this but how would I customize my tab bar to look like this? 标签栏看起来像这样,选中的标签看起来像中间的图标,我有这个的所有资产,但我如何自定义我的标签栏看起来像这样?

First, you would need the assets in @2x and @3x forms(@1x if you are developing for before iOS 7). 首先,您需要@ 2x和@ 3x表单中的资源(如果您在iOS 7之前开发,则需要@ 1x)。 The icons need to be square with a set of white icons in the sizes below and icons with hex color #2E967E and the background alpha with sizes: 图标需要是方形的,下面的尺寸是一组白色图标,图标是十六进制颜色#2E967E ,背景alpha是大小:

  • icon@2x = 50x50 icon @ 2x = 50x50
  • icon@3x = 75x75 icon @ 3x = 75x75

You would need to go to images.xcassets and create six new image sets. 您需要转到images.xcassets并创建六个新图像集。 3 of the sets will be for white icons and three for colored. 其中3套用于白色图标,3套用于彩色。 For all six image sets, go to the rightmost icon(which looks like a slider) in the attributes inspector and change render mode to "Original Image" 对于所有六个图像集,请转到属性检查器中最右边的图标(看起来像滑块),并将渲染模式更改为“原始图像”

Now go to your tab bar controller, select the tab items, and type the name of the image respective white in the "Image" section, and then select it. 现在转到选项卡栏控制器,选择选项卡项,然后在“图像”部分中键入相应白色图像的名称,然后选择它。 Then do the same for the selected image, except this time, chose the colored icon. 然后对所选图像执行相同操作,除非此时选择彩色图标。 Delete the text label, and go to the ruler section and modify the top and bottom image insets(where the top one = negative bottom one) to make the image centered. 删除文本标签,然后转到标尺部分并修改顶部和底部图像插入(顶部的一个=负底部)以使图像居中。

Finally, go to the tab bar(by clicking Tab Bar Controller Scene -> Tab Bar Controller -> Tab Bar in the Document Outline), and go to the attributes inspector and click bar tint, and change the hex code to 47E9C3 . 最后,转到选项卡栏(通过单击文档大纲中的选项卡栏控制器场景 - >选项卡栏控制器 - >选项卡栏),然后转到属性检查器并单击栏色调,并将十六进制代码更改为47E9C3 Uncheck translucent. 取消选中半透明。

Now you should be done! 现在你应该完成!

Try this code in applicationDidFinishLaunchingWithOption. 在applicationDidFinishLaunchingWithOption中尝试此代码。 "tabbarbg.png" image height=49. “tabbarbg.png”图片高度= 49。

 UITabBar.appearance().tintColor = UIColor.lightGrayColor()//selected tab color
 UITabBar.appearance().backgroundImage = UIImage(named:"tabbarbg.png")
 UITabBar.appearance().barTintColor = UIColor.whiteColor()

您需要在单独的软件中自己创建图标,然后在Images.xcassets中导入它们,然后使用main.storyboard应用它们。

It is very easy to set up the image for the tab bar view controller all you need is different images for the tabs that you want to use. 为标签栏视图控制器设置图像非常容易,您只需要为要使用的选项卡设置不同的图像。 first of all open up your story board and select the tab [ie., tab bar view controller followed by navigation controller followed by view controller like this if u set up you will get the tabs] and provide the image in the bar item.. It is shown in the image 首先打开你的故事板并选择标签[即,标签栏视图控制器,然后是导航控制器,然后是视图控制器,如果你设置你将获得标签]并在条形项中提供图像。它显示在图像中 在此输入图像描述

And if you want to make the selected tab with different image just describe that in app delegate 如果您想使用不同的图像制作选定的选项卡,只需在app delegate中描述

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"image1"]];}

this piece of code for changing the selected tab is in objective C 用于更改所选选项卡的这段代码在目标C中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM