Use UITabBar without UITabBarController to control UIWebView

I want to use UITabBar to allow the user to navigate one UIWebView . Is this possible, or am I better off using UIToolbar ? If it’s better to use UIToolbar , how would I simulate the "selected" visual state of a button, is the UITabBar method initially implemented?

+6
source share
4 answers

UITabBar without UITabBarController to control UIWebView

I installed Objective-C as well as a version of Swift to solve this problem. The only thing you really need to do is implement the UITabBarDelegate protocol for the class. It should not be a ViewController, but in this simple example it is).

Objective-C in Xcode 5

In this Objective-C example, you can switch between the three search engines from UITabBar.

First you must configure your views as follows: (You cannot set the delegate output unless you first add a UITabBarDelegate to the ViewController, see Code)

enter image description here

Now, since you do not have a direct link to the elements inside the UITabBar, because we installed it in Interface Builder, you need to set tags on them. I'm not a big fan of this method, but if you take care, everything works fine. If you want to configure something more complex using the dynamic load buttons, you should consider subclassing UITabBar.

enter image description here

Now it’s important that the “tag” setting is on the right. You need to set this number to each of the buttons. In this case, I set them to 0, 1 and 2, since this more or less correlates with how the array is counted, but you can choose any number.

Having established this, we can take a look at the code:

.h file:

 #import <UIKit/UIKit.h> @interface LVDViewController : UIViewController<UITabBarDelegate> // implementing the delegate allows you to drag and drop the delegate reference from the first screenshot @property (weak, nonatomic) IBOutlet UIWebView *webView; @property (weak, nonatomic) IBOutlet UITabBar *tabBar; @end 

.m file:

 #import "LVDViewController.h" @interface LVDViewController () @end @implementation LVDViewController - (void)viewDidLoad; { [super viewDidLoad]; // Set the first item as selected. It will automatically load Google [self tabBar:self.tabBar didSelectItem:[self.tabBar.items firstObject]]; self.tabBar.selectedItem = [self.tabBar.items firstObject]; // This is optional } - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; { NSString *searchEngineURLString = nil; switch (item.tag) { case 0: searchEngineURLString = @"https://google.com"; break; case 1: searchEngineURLString = @"https://duckduckgo.com"; break; case 2: searchEngineURLString = @"https://bing.com"; break; } [self loadSearchEngine:searchEngineURLString]; } - (void)loadSearchEngine:(NSString *)searchEngineURLString; { NSURL *searchEngineURL = [NSURL URLWithString:searchEngineURLString]; NSURLRequest *searchEngineRequest = [NSURLRequest requestWithURL:searchEngineURL]; [self.webView loadRequest:searchEngineRequest]; } @end 

Quick Solution in Xcode 6

We now have a version of Objective-C working on it for the Swift version. First, we set up our View in Interface Builder again, this time Bing is a favorite search engine, not Google, to reflect Apple’s new point of view. It is almost identical, and you also need to add tags to the elements of the tab bar again.

enter image description here

We have our view controller again, but now we use Swift, so it only has one file:

 import UIKit class ViewController: UIViewController, UITabBarDelegate { @IBOutlet var tabBar : UITabBar @IBOutlet var webView : UIWebView override func viewDidLoad() { super.viewDidLoad() loadSearchEngine("https://www.bing.com") tabBar.selectedItem = tabBar.items[0] as UITabBarItem; } func tabBar(tabBar: UITabBar!, didSelectItem item: UITabBarItem!) { var searchEngineURLString: NSString! = ""; switch item.tag { case 0: searchEngineURLString = "https://www.bing.com" break case 1: searchEngineURLString = "https://www.duckduckgo.com" break case 2: searchEngineURLString = "https://www.google.com" break default: searchEngineURLString = "https://www.bing.com" break } loadSearchEngine(searchEngineURLString); } func loadSearchEngine(searchEngineURLString: NSString!) { var searchEngineURL = NSURL(string: searchEngineURLString) var searchEngineURLRequest = NSURLRequest(URL: searchEngineURL) webView.loadRequest(searchEngineURLRequest) } } 
+22
source

Using platform-style users is familiar with your application - that’s fine, but if you want more flexibility in where you place things and how you set up your hierarchy, you can simply go with UITabBar directly and then implement UITabBarDelegate separately.

https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBar_Class/Reference/Reference.html#//apple_ref/doc/c_ref/UITabBar

I always advocate not using the UITableViewController and other specialized ViewControllers, such as the UITabBarController, because sharing everything avoids interference and makes your applications more flexible.

+1
source

According to Apple docs , UITabBar not intended to install navigation tools for a single view, such as your UIWebView , but to navigate between different views. Therefore, it is better to use UIToolBar .

+1
source

I had to add a link for the delegate to the viewDidLoad () function:

 self.tabBar.delegate = self 

Otherwise, the func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) function func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) will never be called.

+1
source

Source: https://habr.com/ru/post/970842/


All Articles