[英]How to change the UItabbar icon image?
I am working on an app and want to customize the UItabbar icon image.我正在开发一个应用程序并想要自定义 UItabbar 图标图像。
I have an image with name about.png this image i want to set as left icon image of my app's UItabbar.我有一个名为 about.png 的图像,我想将此图像设置为我的应用程序 UItabbar 的左图标图像。 how can i do this?
我怎样才能做到这一点?
k you use this code and used your own images not the built in one's used your images... k您使用此代码并使用您自己的图像,而不是内置的使用您的图像...
- (id)init {
UIImage* anImage = [UIImage imageNamed:@"newspaper.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"News" image:anImage tag:0];
self.tabBarItem = theItem;
[theItem release];
return self; }
newspaper.png is my own image in the tab bar...报纸.png 是我自己在标签栏中的图像...
k fine now this will be sufficient for your problem...现在很好,这足以解决您的问题...
You can change it like.你可以改变它。 in tabbar controller delegate method
在标签栏中 controller 委托方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if([tabBarController selectedIndex] == 0)
{
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
}
}
through this you can change your tabbaritem image.通过这个你可以改变你的 tabbaritem 图像。
Or you can use directly in your view controllers init(or ViewWillAppear) method, like或者您可以直接在视图控制器中使用 init(或 ViewWillAppear)方法,例如
[viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
First you make class this code:首先你制作 class 这个代码:
extension UITabBarController {
func addIcon(icon : UIImage, deselectIcon : UIImage, buttonIndex : Int, currentPage : Int){
var index = 0
for view in view.subviews {
if view.subviews.count > 3 {
for _view in view.subviews {
if index == buttonIndex {
for v in _view.subviews {
if v is UIImageView {
v.removeFromSuperview()
}
}
let img = UIImageView(frame: CGRect(x: _view.frame.width / 2 - 15, y: 4, width: 30, height: 30))
if buttonIndex == currentPage {
img.image = icon
}else{
img.image = deselectIcon
}
img.contentMode = .scaleAspectFit
_view.addSubview(img)
}
index += 1
}
}
}
}
}
In didload
you call function:在
didload
,您调用 function:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1 ) {
self.tabBarController?.addIcon(icon: #imageLiteral(resourceName: "Shop"), deselectIcon: #imageLiteral(resourceName: "Shop"), buttonIndex: 1, currentPage: 1)
}
}
override func viewWillAppear(_ animated: Bool) {
tabBarController?.addIcon(icon: #imageLiteral(resourceName: "Shop"), deselectIcon: #imageLiteral(resourceName: "Shop"), buttonIndex: 1, currentPage: 1)
}
If you want to change the image for UITabbarItem you can make use of its instance method如果要更改UITabbarItem的图像,可以使用其实例方法
- (id)initWithTitle:(NSString *)title image:(UIImage *)image tag:(NSInteger)tag
Implement init() in viewController in which you are using tab.在您使用选项卡的 viewController 中实现 init()。
- (id)init {
if (self = [super initWithNibName:@"Search" bundle:nil]) {
UIImage* tabImage = [UIImage imageNamed:@"search.png"];
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Search" image:tabImage tag:0];
self.tabBarItem = theItem;
[theItem release];
}
return self;
}
There are always two ways of doing this by programmatically or by using visually(using IB)通过编程或视觉使用(使用 IB)总是有两种方法可以做到这一点
Programmatically:-以编程方式: -
UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"News" image:anImage tag:0];
use this or if you are doing visually:- Click on the particular tab icon in the IB and chose customize and icon and give the name of particular icon file.使用这个或者如果你在视觉上做:- 点击 IB 中的特定选项卡图标并选择自定义和图标并给出特定图标文件的名称。
May this will solve your problem...愿这能解决你的问题...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.