Can a UIView object combine the accessibility properties of its children automatically? - uikit

I have a view hierarchy as follows:
UIView
\-- UIStackView
+-- UILabel
\-- UILabel
By default, VoiceOver skips over the UIView and stops on each of the UILabels separately.
I would like VoiceOver to stop on the UIView and combine its contents (i.e. read the accessibility label of each of its accessible children one by one). Does UIKit offer an API to achieve this similar to SwiftUI's accessibilityElement(children: .combine) API?
Notes
I know I can set isAccessibilityElement to true on the UIView and then set its accessibilityLabel to "\(label1.text!) \(label2.text!)". I'm wondering whether there is an automatic means of achieving this which doesn't require me to define the UIView's accessibilityLabel.
The problem with having to define the UIView's accessibilityLabel manually based on its children is that I have to remember to change the UIView's accessibilityLabel in every place where I change the text or accessibilityLabel of any of its children.

UIKit does not offer an API like SwiftUI's accessibilityElement(children: .combine) for merging the accessibility labels of all non-hidden children automatically into the parent. I have submitted a suggestion for this in Feedback Assistant though I am doubtful it will ever see the light of day.
As suggested by an Apple Frameworks Engineer in the Apple Developer Forums here, a workaround is to update the parent view's accessibility label whenever the text in any of its children changes by means of the stack view that sits between the parent and its children, as follows:
parentView.accessibilityLabel = stackView.arrangedSubviews
.map { $0.accessibilityLabel ?? "" }
.joined(separator: " ")

Related

NSSplitView Holding Priorities not shown in IB, why?

I am puzzled and could swear I set my NSSplitView holding priorities in IB some days ago, but now they don't seem to be visible in IB at all. Does anyone else see holding priorities in the top left of Xcode's IB when the splitView is selected ?
Does your split view have subviews? I can reproduce what you're seeing if I delete all subviews from a split view. That section shows one slider for each subview.
Update:
You are using the new NSSplitViewController and NSSplitViewItem classes introduced with Yosemite. Because of that, your split view does not, in fact, have subviews in the NIB. (There's no triangle toggle to disclose subviews under the split view.) The split view items will provide views on demand via their view controllers.
The holding priority is a property of the split view item. I can't verify at the moment, but I expect that Xcode will provide a means to set it if you select the individual items.

UIPage Control custom style NatGeo

In my app I have UIView that flow through the use of a horizontal scrollView. To scroll the view I used the classimo method [self addChildViewController: [self.storyboard instantiateViewControllerWithIdentifier: # "name Storyboard ID"]];
Each UIView is followed through the use of UIPageControl classic, but I do not like it and so I wanted to create something like this (see photo)
as you can see from the images above application there is a menu with a triangle pointing down, Indicating the page, passing from one topic to another user through the horizontal swipe on the scrollView
In other words, instead of having the classic shot for the management of the pages, the National Geographic has used a triangle pointing down and the title of the page on which the user is ...
Could someone help me understand how can 'be created a similar PageControl?
There are a lot of approaches to implement this. Maybe the custom PageControl won't be the subclass of a UIPageControl.
For example, you can subclass a UIView, and put a UIScrollView in it. And then put a bunch of UIButtons in a row into the UIScrollView, each of them has a title that is your page's title. When you tap a button ( you can get this event by UIButton's -addTarget:action:forControlEvents:), you can scroll the tapped button to the right position, highlight it, and put a little triangle below it.
There are many possibilities. Say, you can replace the buttons with UILabel and add UITapGestureRecognizer to capture the users tap, or you can replace the UIScrollView with UITableView if you get a long list of pages(Of course that will introduce some complexity). You just need to pick your favorite and try it out.

Xcode - Nested storyboard views - hide/show?

I have a reasonably complicated UIView which contains several nested views which are displayed according to a variety of responses - all are laid out in a storyboard.
Is there a way to hide a view in the foreground to work on a view in the background? As its really fiddly selecting particular elements to arrange / style!?
I've been trying to figure out a nice way to do this, some function like hiding the view and its subviews from the storyboard (not from the actual application), but couldn't find anything.
This is not the nicest of ways but it is how I do it at the moment...
What I do is select the views I want to "hide" from the document outline and add a constant (screen width/height) value to its x/y origin value to push them out of the screen. I also change the document label for those views (Identity Inspector > Document > Label) to something like "Hidden" so I can later search for the "hidden" views from the document outline and put them back where they belong.
I have 2 work arounds.
A) Change the View Controllers size to freeform. Set its size to be really large so I can space out the views.
B) Use the sort order of the views Document Outline (lowest is front most) and add an image view (same as the view background) under the first view to block the others. Then delete it after finishing my edits.
or xcode developers could just simple add a design-visible checkbox for views and controls.... but ill take my rants somewhere else.
In xcode 7 you can do it from the storybord
for more details
https://stackoverflow.com/a/25213491/4879683
Maybe this could help you :
Open your storyboard in the Finder and edit it with a simple text editor (not xcode).
You will see it's just a xml file.Look for the view you want to hide, and add hidden="YES" in the parameters list.
That's what I do on my own project.
In Xcode when you select your storyboard, you have a panel that displays all your view controllers and their hierarchy. If you change the order of the elements you change the background/foreground order.
You can add extra views to the scene dock.
These views get initiated along with the view controller, but are not added to the view controller's view hierarchy. You can reference them using IBOutlets.
e.g. I have a full screen loading view that I added to the scene dock instead of covering up the view controller in the storyboard. I can add the loading view to the view controller's view hierarchy in code:
#IBOutlet weak var loadingView: UIView!
...
loadingView.frame = view.bounds
loadingView.autoresizingMask = UIViewAutoresizing.FlexibleWidth.union(UIViewAutoresizing.FlexibleHeight)
view.addSubview(loadingView)
Reference: https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/Chapters/AddViewsToDock.html

Removing Border from NSBrowser

As the title says, I actually want to remove the border from an NSBrowser control. The parents are NSView and NSControl. There's no available methods in NSBrowser itself, and neither in the parent controls. This one seems completely undocumented.
As to the reason for removing the border, because it's programatically docked into another view, so the view hierarchy means there's a border already.
Any ideas?
Just embed it a plain NSView ("Custom View" in IB) and make the browser's frame "outset" by 1 point in all directions from the containing view's bounds. The containing view will clip the browser to eliminate the border. Then place that containing view into the surrounding view hierarchy, rather than adding the browser directly.
NSBrowser uses NSBrowserCell to implement its interface.
That inherits from NSCell.
NSCell should provide the methods you need.
From NSCell class reference...
Managing Display Attributes
– setBezeled:
– isBezeled
– setBordered:
– isBordered
– isOpaque
– setControlTint:
– controlTint
– setBackgroundStyle:
– backgroundStyle
– interiorBackgroundStyle
Remove the border on the NSScrollView that it is enclosed in.
Neither of existing answers worked for me.
Embedding in NSView looked like an acceptable workaround, but it clips the browser.
NSScrollView isn't superclass of NSBrowser.
Layer is not used for this border, it's nil.
NSBrowserCell has nothing to do with NSBrowser's border.
Use borderType: NSBorderType property (same as in some other classes), setting it in Interface Builder. In Identity Inspector (⌘+⎇+3) add to User Defined Runtime Attributes:
Key Path: borderType
Type: Number
Value: 0 (corresponds to NSBorderType.noBorder)
For those, who still looking for a solution. NSBrowser is the subclass of NSView. You can set an NSView layer's border width to 0:
(Swift)
self.layer?.borderWidth = 0

Can I disable autolayout for a specific subview at runtime?

I have a view that needs to have its frame manipulated programmatically - it's a kind of document view that wraps to its content which is then scrolled and zoomed around a superview by manipulating the frame origin. Autolayout fights with this at runtime.
Disabling autolayout completely seems a bit harsh because it could reasonably be used to handle layout for the other views. It seems like what I might want is some kind of "null constraint".
I had the same problem. But I have resolved it.
Yes, you can disable auto layout at runtime for a specific UIView, instead of disabling it for the whole xib or storyboard which is set by default in Xcode 4.3 and later.
Set translatesAutoresizingMaskIntoConstraints to YES, before you set the frame of your subview:
self.exampleView.translatesAutoresizingMaskIntoConstraints = YES;
self.exampleView.frame = CGRectMake(20, 20, 50, 50);
I had a similar issue where Autolayout was overriding some of my frame-setting at run time (I had a dynamic view that in some cases pushed a new view controller...pushing and then pressing Back would reset the initial view).
I got around this by putting my manipulation code in viewDidLayoutSubviews of my View Controller. This seems to get called after whatever constraint mojo gets called, but before viewDidAppear, so the user is none the wiser.
Perhaps just setting translatesAutoresizingMaskIntoConstraints to YES (and not adding additional constraints affecting that view) will let you set the frame without fighting the auto layout system.
In iOS 8 you can set an NSLayoutConstraint to be active or not. So if I'm using interface builder, I add all my constraints to an OutletCollection and then activate or deactivate using:
NSLayoutConstraint.deactivateConstraints(self.landscapeConstraintsPad)
NSLayoutConstraint.activateConstraints(self.portraitConstraintsPad)
The particular application I'm using it for here is having different constraints in portrait and landscape mode and I activate/deactivate based on the rotation of the device. It means I can create some complex layout changes all in interface builder for both orientations, and still use auto layout without the verbose auto layout code.
Or you can activate / deactivate using removeConstraints and addConstraints.
I don't know if this will help anyone else, but I wrote a category to make this convenient because I find myself doing this a lot.
UIView+DisableAutolayoutTemporarily.h
#import <UIKit/UIKit.h>
#interface UIView (DisableAutolayoutTemporarily)
// the view as a parameter is a convenience so we don't have to always
// guard against strong-reference cycles
- (void)resizeWithBlock:(void (^)(UIView *view))block;
#end
UIView+DisableAutolayoutTemporarily.m
#import "UIView+DisableAutoResizeTemporarily.h"
#implementation UIView (DisableAutoResizeTemporarily)
- (void)resizeWithBlock:(void (^)(UIView * view))block
{
UIView *superview = self.superview;
[self removeFromSuperview];
[self setTranslatesAutoresizingMaskIntoConstraints:YES];
__weak UIView *weakSelf = self;
block(weakSelf);
[superview addSubview:self];
}
#end
I use it like this:
[cell.argumentLabel resizeWithBlock:^(UIView *view) {
[view setFrame:frame];
}];
Hope it helps.
You can set the translatesAutoresizingMaskIntoConstraints type Boolean, Value Yes in the User Defined Runtime Attributes of the UIView you want in the xib/storyboard.
In my view I had a Label and a Text. The label had pan gesture. The label moves around fine during drag. But when I use the text box keyboard, the label resets its position to the original location defined in auto layout. The issue got resolved when I added the following in swift for the label. I added this in viewWillAppear but it can be added pretty much anywhere you have access to the target field.
self.captionUILabel.translatesAutoresizingMaskIntoConstraints = true
Open project in 4.5
Select storyboard
Open the file inspector
Under Interface Builder Document uncheck 'Use Autolayout'
You can split across multiple storyboards if you want to use autolayout for some views.
For me it worked to create the subview programmatically, in my case the auto layout was messing with a view that I needed to rotate around its center but once I created this view programmatically it worked.
I've encountered a similar scenario, where I joined a project that was initiated with auto-layout, but I needed to make dynamic adjustments to several views. Here is what has worked for me:
Do NOT have views or components laid out in interface builder.
Add your views purely programmatically starting with alloc/init and setting their frames appropriately.
Done.
This happened to me in a project without storyboards or xib files. All 100% code. I had an ad banner at the bottom and wanted the view bounds to stop at the ad banner. The view would resize itself automatically after loading. I tried every resolution on this page but none of them worked.
I ended up just creating a sub view with the shortened height and placed that in into the main view of the controller. Then all my content went inside the sub view. That solved the problem very easily without doing anything that felt like it was going against the grain.
I am thinking if you want a view that is not the normal size that fills the window then you should use a sub view for that.
Instead of disabling autolayout, I would just calculate the new constraint with the frame you are replacing. That appears to me to be the appropriate way. If you are adjusting components that rely on constraints, adjust them accordingly.
For example, if you have a vertical constraint of 0 between two views (myView and otherView), and you have a pan gesture or something that adjusts the height of myView then you can recalculate the constraint with the adjusted values.
self.verticalConstraint.constant = newMyViewYOriginValue - (self.otherView.frame.origin.y + self.otherView.frame.size.height);
[self.myView needsUpdateConstraints];
For those of you who are using auto layout, please check out my solution here. You should be making #IBOutlet's of the constraints you want to adjust and then change their constants.
if it's xib file:
select the .xib file
select the "File's Owner"
show the Utilities
click on: "File Inspector"
Under "Interface Builder Document" disable: "Use Autolayout"

Resources