UISlider does not appear in the UIToolbar - ios

I have a fairly basic video player I'm creating. There is a UIToolbar (toolBar) that contains several buttons and a UISlider. The slider will appear just fine when the toolBar is not a referencing outlet of the View Controller. Once I link up the toolBar with the view controller, the slider disappears, but all other buttons remain on the toolbar.
I am using a storyboard for the UI, and adding buttons and the slider to the toolbar when the view loads with...
UIBarButtonItem *scrubberItem = [[UIBarButtonItem alloc] initWithCustomView:movieTimeControl];
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBar.items = [NSArray arrayWithObjects:flexItem, currentPositionLabel, scrubberItem, timeLeftLabel, nil];
movieTimeControl is a UISlider property.
I need the toolBar to be an outlet of the controller so that I can dismiss it on specific gestures.
This is how it looks before I link up the toolBar with the controller...
And after linking it with the controller...
So the space for the slider remains, but the slider is not there.
Any thoughts on why the UISlider is disappearing?

Related

Is it possible to add a UIBarButtonItem besides a UISegmentedControl?

I've added a UISegmentedControl to a UINavigation Bar, and when I tried to add a UIBarButtonItem programmatically in viewDidLoad viewController using the following code:
UIBarButtonItem *timeTableBarButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"blueRoundRect"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(popUpTimeTable)];
self.navigationItem.rightBarButtonItem = timeTableBarButton;
There are no button showing at all. I wonder since I already have a UISegmentedControl, is it possible to do so?
It is possible to add the Bar button in navigation bar and there is no way your segment control should be blocking right bar button item. Please make sure that your self.navigationItem is not nil.

How do I fit a UISearchBar in a UIBarButtonItem?

I'm using a UISearchController in iOS 8 and when I make its UISearchBar my table's header view everything is fine. However now I need it in my UINavigationBar's left bar button (can't be the title view because of the vertical centering when I enlarge the nav bar). When I wrap the UISearchBar in a UIBarButtonItem it is larger than the width of the screen.
I think it is related to my view controller initialized from a storyboard with size classes enabled which means my frames are not set till viewDidLayoutSubviews. However I had the same issue with my segmented control in a toolbar and I just called sizeToFit on the toolbar in viewDidLayoutSubviews and that fixed the toolbar. When I do the same for the search bar it still draws partly off screen. Apple does this in some of their iPad apps but how?
Note: I can hack it to get it all on screen but I have to wrap it in another view but then the color is off and it just seems wrong and I think the animation is off.
You need to create a UIBarButtonItem with UIBarButtonSystemItemFixedSpace with -10 width which will remove 10 px padding.
UIBarButtonItem *Spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[Spacer setWidth:-10];
UISearchBar *searchBar= [[UISearchBar alloc] initWithFrame:CGRectMake(0, 5, self.view.frame.size.width-10, 39)];
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:Spacer,searchBarItem,nil];

UIBarButtonItem with custom view flexibly sized

Is it somehow possible to get a UIBarButtonItem with a custom view containing a UISlider to automatically resize similarly to a UIBarButtonItem using the system style UIBarButtonSystemItemFlexibleSpace?
For example (for the sake of brevity I've left out setting frames and adding the toolbar as a subview to an existing view)
UISlider *slider = [UISlider new];
slider.minimumValue = 0;
slider.maximumValue = 1;
slider.value = 0.5
UIBarButtonItem *sliderItem = [[UIBarButtonItem alloc] initWithCustomView:slider];
UIBarButtonItem *exampleItem = [[UIBarButtonItem alloc] initWithTitle:#"Example" style:UIBarButtonItemStylePlain target:nil action:nil];
UIToolbar *toolbar = [UIToolbar new];
toolbar.items = #[sliderItem, exampleItem];
The idea being the toolbar is the full width of the screen, exampleItem can be a variable width due to localisation of it's title, and slideItem resizes similarly to UIBarButtonSystemItemFlexibleSpace to take up the remaining space in the toolbar.
But the results are instead the slider stays taking up what ever frame you've given it (or the default/minimum frame if left out) and the next button just sits up against it.
I've tried the method mentioned in the following answer make a UIBarButtonItem with customView behave like Flexible-Space item by changing the auto resizing mask of the slider, but it appears not to work (the question is about using a UITextField not a UISlider which is probably why.)
I've also tried using another UIView containing the UISlider as the custom view to intercept any sizeThatFits calls, but it in fact never gets called as I suspected.

Position Toolbar on top of the screen with correct shadow in the bottom

I have navigation controller based app and one viewcontroller presents modally graph in a landscape mode. I then add Toolbar with Done button to dismiss the graph vc and return to navigation and portrait mode.
I can't figure out how to position the Toolbar on top of the graph viewcontroller with correct shadow on the bottom of the toolbar. So far I have this code to add the toolbar to the bottom position, which has default shadow on the top of the toolbar. Is it allowed to have toolbar on top of the screen? For the reason of forced orientation rotation I cannot use navigation controller with the graph vc. Platform is iOS7 and iPhone only. Thanks.
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.size.width - 44.0, self.view.bounds.size.height, 44.0)];
UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done)];
toolbar.items = [NSArray arrayWithObjects:flexibleSpaceButtonItem, doneButtonItem, nil];
[self.view addSubview:toolbar];
I think your frame is looks a bit strange. You are calculating the y position from the view width and the width from the view height.
Maybe you have to specify that the toolbar is on top using the UIBarPositioning protocol.
UIImage *shadow = [toolbar shadowImageForToolbarPosition: UIBarPositionAny];
[toolbar setShadowImage:shadow forToolbarPosition:UIBarPositionTopAttached];
Next Edit:
This is what the documentation has to say about the iOS 7 UIToolbar:
UIBarPositionTop
Specifies that the bar is at the top of its containing view.
The system uses this as a hint to draw directional decoration accordingly. For example, any shadow would be drawn below the bar.
Instances of UIToolbar do not appear with this position on iPhone, but they can on iPad.
Available in iOS 7.0 and later.
Declared in UIBarCommon.h.
Maybe toolbars are not meant to be used on top. However, you can simply add a shadow with addSubview:
Try to implement the method
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
from of UIToolbarDelegate protocol.

Add goBack and goForward to the right of the Navigation Bar

I've a WebviewViewController and a navigation bar with the Back Bar Button. Now I want two buttons on the right of the bar, a goBack and a goForward. I've searching for a while and none of the answers works properly.
There's a way to add them in the storyboard or only programmatically? If so, how do I do it?
I did it in the past, it's easy:
myBackBarButton = [[UIBarButtonItem alloc] initWithImage:barButtonImage
style:UIBarButtonItemStylePlain
target:myWebView
action:#selector(goBack)];
myForwardButtom = [[UIBarButtonItem alloc] initWithImage:anotherBarButtonImage
style:UIBarButtonItemStylePlain
target:myWebView
action:#selector(goForward)];
Where myWebView is your UIWebView (In case it's your class, use self)
If your buttons are already created, then just add the target and the action to them:
myBackButton.target = myWebView;
myBackButton.action = #selector(goBack);
What you are doing is adding the action goForward: and goBack: of the target that is your webview, to those buttons.

Resources