![]() ![]() With the new progressButton item created, we can put it into our toolbar items anywhere we want it. The last line creates a new UIBarButtonItem using the customView parameter, which is where we wrap up our UIProgressView in a UIBarButtonItem so that it can go into our toolbar.The second line tells the progress view to set its layout size so that it fits its contents fully.bar, which doesn't draw an unfilled line to show the extent of the progress view, but the default style looks best here. The first line creates a new UIProgressView instance, giving it the default style.Let progressButton = UIBarButtonItem(customView: progressView)Īll three of those lines are new, so let's go over them: Now place this code directly before the let spacer = line in viewDidLoad(): progressView = UIProgressView(progressViewStyle. Begin by declaring the property at the top of the ViewController class next to the existing WKWebView property: var progressView: UIProgressView! So, we're going to ask iOS to tell us using a powerful technique called key-value observing, or KVO.įirst, let's create the progress view and place it inside the bar button item. Although WKWebView tells us how much of the page has loaded using its estimatedProgress property, the WKNavigationDelegate system doesn't tell us when this value has changed.Instead, you need to wrap them in a special UIBarButtonItem, and use that instead. You can't just add random UIView subclasses to a UIToolbar, or to the rightBarButtonItem property. ![]() However, this requires two new pieces of information: The next step is going to be to add a UIProgressView to our toolbar, which will show how far the page is through loading. That code will compile and run, and you'll see the refresh button neatly aligned to the right – that's the effect of the flexible space automatically taking up as much room as it can on the left. The second sets the navigation controller's isToolbarHidden property to be false, so the toolbar will be shown – and its items will be loaded from our current view. The last two lines are new: the first creates an array containing the flexible space and the refresh button, then sets it to be our view controller's toolbarItems array. The second line you've seen before, although now it's calling the reload() method on the web view rather than using a method of our own. ![]() It doesn't need a target or action because it can't be tapped. flexibleSpace, which creates a flexible space. The first line is new, or at least part of it is: we're creating a new bar button item using the special system item type. NavigationController?.isToolbarHidden = false refresh, target: webView, action: #selector(webView.reload)) Let refresh = UIBarButtonItem(barButtonSystemItem. In viewDidLoad(), put this new code directly below where we set the rightBarButtonItem: let spacer = UIBarButtonItem(barButtonSystemItem. This is a unique UIBarButtonItem type that acts like a spring, pushing other buttons to one side until all the space is used. We're going to create two UIBarButtonItems at first, although one is special because it's a flexible space. All we need to do is set the array, then tell our navigation controller to show its toolbar, and it will do the rest of the work for us. ![]() This is very similar to the way rightBarButtonItem is shown only when the view controller is active. The way we're going to use UIToolbar is quite simple: all view controllers automatically come with a toolbarItems array that automatically gets read in when the view controller is active inside a UINavigationController. UIProgressView is a colored bar that shows how far a task is through its work, sometimes called a "progress bar." We already saw how each view controller has a rightBarButton item, so a UIToolbar is like having a whole bar of these items. UIToolbar holds and shows a collection of UIBarButtonItem objects that the user can tap on. Now is a great time to meet two new UIView subclasses: UIToolbar and UIProgressView. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |