iOS Navigation Bar Prefers Large Titles Scroll Behaviour - cocoa-touch

In iOS 11 the system apps all compress the navigation bar as you scroll down if you enable prefersLargeTitles:
I can't figure out how to implement this in my own apps though, the bar stays the same by default:
The only thing I can see is Hide Bars On Swipe, but that hides the whole bar rather than compressing it:
This is just an empty project created in Xcode 9 beta and with a new storyboard added.
What do I need to do to get the same behaviour as the system apps?

Don't set anything regarding Large Titles in Interface Builder / Storyboard, only in code. That worked for me.
So in the navigation bar in storyboards, Prefers Large Titles unchecked.
In your view controller:
self.navigationController?.navigationBar.prefersLargeTitles = true

It seems like this issue is happening to people for different reasons. None of the above answers helped me, but here's what DID work...
I deconstructed my app to find the cause, which was the view hierarchy in the storyboard. It appears that the UITableView view HAS to the the first view in your view controller. I had a UITableView with two UIImageViews behind it and that's what was causing the issue. Once I removed those UIImageViews everything worked correctly.
My fix: I ended up creating a UIView in code, adding my two image views to that, THEN adding that UIView to the UITableview.backgroundView.
Hope this helps someone.

If you have to target older iOS versions, you’ll also have to wrap the assignment in an availability check:
if #available(iOS 11, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
}

if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.topItem?.title = "Hello"
navigationController?.navigationItem.largeTitleDisplayMode = .automatic
let attributes = [
NSAttributedStringKey.foregroundColor : UIColor.red,
]
navigationController?.navigationBar.largeTitleTextAttributes = attributes
} else {
// Fallback on earlier versions
}
http://iosrevisited.blogspot.in/2017/09/navigation-bar-with-large-titles-and.html

Related

iOS UI Testing cannot see the UILabel

I have a UI testing issue with some code I've got from another developer (Whose not with the project anymore). The code display a simple alert box at the top of the screen when there's an error and I cannot get my UI test to see the UILabel within it.
The structure of the box looks like this:
UIView
+ UILabel
+ UIButton
And in the custom class for the UIView there is this code:
// Original code
accessibilityIdentifier = "AlertBar" // Required for UI Testing.
// Bits I've added trying to make messageLabel visible to UI Test
isAccessibilityElement = true
accessibilityElements = [messageLabel]
messageLabel.accessibilityIdentifier = "AlertBarMessage"
messageLabel.isAccessibilityElement = true
}
If I breakpoint in the middle of my UI Test and dump the app into the log I can see the UIView:
Attributes: Application, pid: 60317, label: 'The App'
Element subtree:
→Application, 0x600001df8c40, pid: 60317, label: 'The App'
Window (Main), 0x600001dfa060, {{0.0, 0.0}, {390.0, 844.0}}
... app ui logged here!
Other, 0x600001dce220, {{4.0, 47.0}, {382.0, 107.7}}, identifier: 'AlertBar'
So it's clear that the alert is visible to XCTest, but nothing inside it. As you can tell from my added code I've been trying all sorts of things but so far I've not been able to make the UILabel (AlertBarMessage) visible.
What am I missing?
As per usual, after typing up a stackOVerFlow question I go back to the code and figure out the problem :-) In this case it was the isAccessibilityElement = true on the view. Making it accessible was effectively hiding the nested UILabel so turning it off fixed my test.
I know there are ways in Accessibility to setup the view correctly for accessibility so that screen readers see it as a single element and can read the message, but I'll have to work on that later for now.

Xamarin.Forms - Looking for a Popover

I'm working in a Xamarin.Forms project (iOS & Android) that needs something similar to the Bootstrap Popover. I didn't find something similar in the docs.
Q:
Does anyone know a Nuget Package or something similar that in order to include that UI component to a Xamarin.Forms project?
PS: Yes, I tried searching in Google and I could not found a solution.
I think Rg.Popup is a good solution...
// Use these methods in PopupNavigation globally or Navigation in your pages
// Open new PopupPage
Task PushAsync(PopupPage page, bool animate = true) // Navigation.PushPopupAsync
// Hide last PopupPage
Task PopAsync(bool animate = true) // Navigation.PopPopupAsync
// Hide all PopupPage with animations
Task PopAllAsync(bool animate = true) // Navigation.PopAllPopupAsync
// Remove one popup page in stack
Task RemovePageAsync(PopupPage page, bool animate = true) // Navigation.RemovePopupPageAsync
Try out CPOP. It's a popup plugin for Xamarin.Forms (iOS, Android, UWP). It's not the prettiest of the popups, but you can modify the colors. (Also, I think this is the only popup plugin available out there)
NuGet Package: https://www.nuget.org/packages/Calvagna.CPop/
Hope it helps!
I'd try SlideOverKit, it might do more than you need, but it has a nice range of ways of doing popover type things.

Appearance of NavigationBar

This is my very first question to the helpful group of stackoverflow.com
Please bear with me if question framing is cumbersome!!
I have a collectionView(in a ViewController),embedded in a NavigationViewController.
I have used didSelectItemAtIndexPath for each of the collectionView cells, linking them to different viewControllers,say VC1,VC2 etc
I have hidden the Navigation bar, in the ViewController containing the collectionView, using the code
self.navigationController?.navigationBar.hidden = true
In each of VC1,Vc2....., I have tried to unhide the navigationBar using the code,
self.navigationController?.navigationBar.hidden = False
During simulation, using xCode, the navigation bar appears only in VC1, but not in VC2,VC3....
From the details you provided it is hard to guess what is exactly the issue.
The navigation controller will remember its status, as long as you use push segues, it should stay hidden, unless you set it to show again. You can set it to be hidden before you perform the transition, say in didSelectItemAtIndexPath.
To hide the navigation controller, you can use:
navigationController?.setNavigationBarHidden(true, animated: true)
and to show it
navigationController?.setNavigationBarHidden(false, animated: true)

Can I preload the web content for Safari View Controller?

I can create Safari View Controller without problem:
let svc = SFSafariViewController(URL: NSURL(string: remote_url)!, entersReaderIfAvailable: true)
self.presentViewController(svc, animated: true, completion: nil)
Is there any way I can preload the URL before I present the view controller to the user?
For example, I can preload the URL (web content) in the background first, and after the user clicks on something, I can show the Safari View Controller with the content right away. The user will feel the page loading is faster or instant.
P.S. Workarounds/hacks are also acceptable. For example, using cache or starting the view controller in background, etc.
EDIT: please consider SFSafariViewController only.
Here is a solution.
Obviously, if you click on the button right away you'll see the loading.
But basically, I load the Browser and put the view behind another one and I put a button in this other view.
When you press the button, the browser is bring to the front, already loaded.
The only problem here is that I'm not using any transition but that's one solution at least.
import UIKit
import SafariServices
class ViewController: UIViewController {
var svc = SFSafariViewController(URL: NSURL(string: "https://microsoft.com/")!, entersReaderIfAvailable: true)
var safariView:UIView?
let containerView = UIView()
let btn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
//let tmpView = svc.view
addChildViewController(svc)
svc.didMoveToParentViewController(self)
svc.view.frame = view.frame
containerView.frame = view.frame
containerView.backgroundColor = UIColor.redColor()
safariView = svc.view
view.addSubview(safariView!)
view.addSubview(containerView)
btn.setTitle("Webizer", forState: UIControlState.Normal)
btn.titleLabel!.textColor = UIColor.blackColor()
btn.addTarget(self, action: "buttonTouched:", forControlEvents: .TouchUpInside)
btn.frame = CGRectMake(20, 50, 100, 100)
containerView.addSubview(btn)
view.sendSubviewToBack(safariView!)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func buttonTouched(sender: AnyObject) {
view.bringSubviewToFront(safariView!)
//self.presentViewController(svc, animated: true, completion: nil)
}
}
Sadly this behaviour is not supported with the current implementation of SFSafariViewController. I would encourage filing a radar with Apple to add support for this behaviour but like others have suggested your best bet is to use WKWebView and start loading before its added to the hierarchy.
I came across a lovely radar from Twitter that actually mentions exactly what you're asking for. I think you might find the following requests useful:
High Priority:
- Ability to warm the SFSafariViewController before actually presenting it with a URL, URL request, HTML data or file on disk
- Currently, are investing heavily into warming the shared URL cache for high priority Tweets so that if the user hits that Tweet we
will open UIWebView (sadly not WKWebView) with that pre-cached web
page. If we could just warm an SFSafariViewController with the
desired link, this would eliminate an enormous amount of effort on our
end.
You can see in their implementation they simply cache responses using UIWebView since WKWebView seems to obfuscate the caching semantics a bit. The only risk is that UIWebView is a likely candidate for deprecation as you see in their docs "In apps that run in iOS 8 and later, use the WKWebView class instead of using UIWebView."
So unfortunately it seems that their are many hoops you need to jump through to get this all going so your best bet for now is to just pester Apple and dupe Twitters radar.
You could try using a http cache, but I don't think it would work as the Safari View Controller is working as a separate process (probably the same as Safari), so that's why it e.g. circumvents ATS.
The only way I can think of this working is to somehow force the user's Safari to load it? openURL: or adding to Reading List maybe? This doesn't sound like a viable solution.
You can always experiment with custom presentation of the view controller, attach it the view hierarchy, trigger appearance events, but set its frame to CGRectMake(0,0,1,1) or attach it somewhere off-screen, then wait a while and represent it with a correct frame.
you can download the web page using the following code . and represent it with the help of svc
let data:NSData?
do {
let weatherData = try NSData(contentsOfURL: NSURL(string: remote_url)!, options: NSDataReadingOptions())
data = weatherData
print(weatherData)
} catch {
print(error)
}
and load it when you needed in the svc
While it's technically possible to use the solution above to achieve what you're asking, this may not pass App Store review. Per the SFSafariViewController docs:
In accordance with App Store Review Guidelines, this view controller must be used to visibly present information to users; the controller may not be hidden or obscured by other views or layers. Additionally, an app may not use SFSafariViewController to track users without their knowledge and consent.

How to toggle visibility of NSSplitView subView + hide Pane Splitter divider?

We have a parent Split view (NSSplitView), and two subviews, Content and SideBar (the sidebar is on the right).
What would be the optimal Cocoa-friendly way to toggle the SideBar view?
I would really love it, if the suggested solution includes animation
I really don't need any suggestions related to external plugins, etc (e.g. BWToolkit)
HINT : I've been trying to do that, but still I had issues hiding the divider of the NSSplitView as well. How could I do it, while hiding it at the same time?
Here's a pretty decent tutorial that shows how to do this: Unraveling the Mysteries of NSSplitView.
Hiding the divider is done in NSSplitView's delegate method splitView:shouldHideDividerAtIndex:.
You will have to animate the frame size change yourself if you don't like the way NSSplitView does it.
Easiest way to do it is as follows - and it's animated: [SWIFT 5]
splitViewItems[1].animator().isCollapsed = true // Show side pane
splitViewItems[1].animator().isCollapsed = false // hide side pane
I wrote a Swift version of the content in the link from #Nathan's answer that works for me. In the context of my example splitView is set elsewhere, probably as an instance property on an encompassing class:
func toggleSidebar () {
if splitView.isSubviewCollapsed(splitView.subviews[1] as NSView) {
openSidebar()
} else {
closeSidebar()
}
}
func closeSidebar () {
let mainView = splitView.subviews[0] as NSView
let sidepanel = splitView.subviews[1] as NSView
sidepanel.hidden = true
let viewFrame = splitView.frame
mainView.frame.size = NSMakeSize(viewFrame.size.width, viewFrame.size.height)
splitView.display()
}
func openSidebar () {
let sidepanel = splitView.subviews[1] as NSView
sidepanel.hidden = false
let viewFrame = splitView.frame
sidepanel.frame.size = NSMakeSize(viewFrame.size.width, 200)
splitView.display()
}
These functions will probably methods in a class, they are for me. If your splitView can be nil you obviously have to check for that. This also assumes you have two subviews and the one at index 1, here as sidePanel is the one you want to collapse.
In Xcode 9.0 with Storyboards open Application Scene select View->Menu->Show sidebar. CTRL-click Show Sidebar, in sent actions delete the provided one, click on x. From the circle CTRL drag to First Responder in application scene and select toggleSideBar to connect to. Open storyboard and select the first split view item and in attributes inspector change behaviour from default to sidebar. Run and try with view menu item show/hide. All done in interface builder no code. toggleSideBar handles the first split view item. https://github.com/Dis3buted/SplitViewController
I got some artifacts with the code above, likely because it was out of context. I am sure it works where it was meant to. Anyway, here is a very streamlined implementation:
// this is the declaration of a left vertical subview of
// 'splitViewController', which is the name of the split view's outlet
var leftView: NSView {
return self.splitViewController.subviews[0] as NSView
}
// here is the action of a button that toggles the left vertical subview
// the left subview is always restored to 100 pixels here
#IBAction func someButton(sender: AnyObject) {
if splitViewController.isSubviewCollapsed(leftView) {
splitViewController.setPosition(100, ofDividerAtIndex: 0)
leftView.hidden = false
} else {
splitViewController.setPosition(0, ofDividerAtIndex: 0)
leftView.hidden = true
}
}
To see a good example using animations, control-click to download this file.
If your NSSplitView control is part of a NSSplitViewController object, then you can simply use this:
splitViewController.toggleSidebar(nil)