簡體   English   中英

如何更改 UItabbar 圖標圖像?

[英]How to change the UItabbar icon image?

我正在開發一個應用程序並想要自定義 UItabbar 圖標圖像。

我有一個名為 about.png 的圖像,我想將此圖像設置為我的應用程序 UItabbar 的左圖標圖像。 我怎樣才能做到這一點?

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;  }

報紙.png 是我自己在標簽欄中的圖像...

現在很好,這足以解決您的問題...

你可以改變它。 在標簽欄中 controller 委托方法

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

{
    if([tabBarController selectedIndex] == 0)
    {
        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
    }    
}

通過這個你可以改變你的 tabbaritem 圖像。

或者您可以直接在視圖控制器中使用 init(或 ViewWillAppear)方法,例如

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];

首先你制作 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
                }

            }
        }

    }
}

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)

}

如果要更改UITabbarItem的圖像,可以使用其實例方法

- (id)initWithTitle:(NSString  *)title image:(UIImage  *)image tag:(NSInteger)tag

在您使用選項卡的 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;
}  

通過編程或視覺使用(使用 IB)總是有兩種方法可以做到這一點

以編程方式: -

UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"News" image:anImage tag:0];

使用這個或者如果你在視覺上做:- 點擊 IB 中的特定選項卡圖標並選擇自定義和圖標並給出特定圖標文件的名稱。

願這能解決你的問題...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM