Swift programmatic UI UIContextMenuInteraction Auto layout error (groupView) on opening context menu - autolayout

I have an app with a programmatic UI (No storyboards). I add a context menu to a button of my viewcontroller. On opening this menu (tap and hold on the button) I get a LayoutConstraints warning/error in the console (menu works fine otherwise:
(
"<NSAutoresizingMaskLayoutConstraint:0x600001c9ed50 h=--& v=--& UIInterfaceActionGroupView:0x7f9d4478ceb0.height == 0 (active)>",
"<NSLayoutConstraint:0x600001cd7c50 groupView.actionsSequence....height >= 66 (active, names: groupView.actionsSequence...:0x7f9d4610ee00 )>",
"<NSLayoutConstraint:0x600001cb3de0 UIInterfaceActionGroupView:0x7f9d4478ceb0.top == _UIContentConstraintsLayoutGuide:0x7f9d4478bba0''.top (active)>",
"<NSLayoutConstraint:0x600001cb3e80 V:[_UIContentConstraintsLayoutGuide:0x7f9d4478bba0'']-(0)-| (active, names: '|':UIInterfaceActionGroupView:0x7f9d4478ceb0 )>",
"<NSLayoutConstraint:0x600001cad090 groupView.actionsSequence....top == _UIContentConstraintsLayoutGuide:0x7f9d4478bba0''.top (active, names: groupView.actionsSequence...:0x7f9d4610ee00 )>",
"<NSLayoutConstraint:0x600001cad130 groupView.actionsSequence....bottom == _UIContentConstraintsLayoutGuide:0x7f9d4478bba0''.bottom (active, names: groupView.actionsSequence...:0x7f9d4610ee00 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600001cd7c50 groupView.actionsSequence....height >= 66 (active, names: groupView.actionsSequence...:0x7f9d4610ee00 )>
this groupView is not a view of mine! I have set translatesAutoresizingMaskIntoConstraints = false on all my custom controls (labels, fields, etc). I presume I need to set that to for the context menu somewhere, but I don't know how or where.
Relevant code:
In my viewcontroller:
let interaction = UIContextMenuInteraction(delegate: self)
annotationTypeButton.addInteraction(interaction)
Delegate extension:
//MARK: - UIContextMenuInteractionDelegate
extension AnnotationDetailsViewController: UIContextMenuInteractionDelegate {
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
return UIContextMenuConfiguration(identifier: "annotationTypeMenu" as NSCopying, previewProvider: nil) { _ in
let children: [UIMenuElement] = self.makeAnnotationTypeActions()
return UIMenu(title: "", children: children)
}
}
func makeAnnotationTypeActions() -> [UIAction] {
var actions = [UIAction]()
for type in AnnotationType.allCases {
actions.append( UIAction(title: type.rawValue, image: type.image, identifier: nil, attributes: []) { _ in
let annotationType = AnnotationType(rawValue: type.rawValue) ?? AnnotationType.tips
self.annotation.type = annotationType
self.configureAnnotationTypeButton(with: annotationType)
})
}
return actions
}
}
Any help appreciated!

I got an answer on the Apple developer forums saying that the layout warning is a known issue and will be solved in iOS14!
https://developer.apple.com/forums/thread/652622?login=true&page=1#618265022

Related

Accessibility for pickerview Voice Over

I need some help, I'm trying to finilize my first application and now I'm implementing the accessibility in orther the application can be used with VoiceOver.
The issu is I have 3 different pickerviews in one view controller but I cannot fix a label for each of them. The properties in the inspector identifier don't work for pickerView. If i use the method:
func pickerView(_ pickerView: UIPickerView, accessibilityLabelForComponent component: Int) -> String?
yes, I fix a label but it is the same for the three of them and I ineed each pickerView have its own label. Each pickerview have one component, but I don't know how to set up in other to differenciate each pickerView and have three different labels, one for each of them.
Any help is wellcome
Many thanks by anticipate
I include here the next:
1st.- a screenshot of one the pickerview selected and how its label in the inspector is setup, but it doesn't work at running:
a screenshot of one the pickerview selected and how its label in the inspector is setup, but it doesn't work at running:
2.- the code used for any other control, label or text field which work for them but not for the pickerview:
extension Rithm {
func applyAccessibility() {
logo.isAccessibilityElement = false
kmMille.subviews[0].accessibilityLabel = NSLocalizedString("Milles", comment: "")
kmMille.subviews[1].accessibilityLabel = NSLocalizedString("Kilometers", comment: "")
switch kmMille.selectedSegmentIndex {
case 1:
distanceTextField.accessibilityLabel = NSLocalizedString("Please, enter a distance in Milles", comment: "")
case 0:
distanceTextField.accessibilityLabel = NSLocalizedString("Please, enter a distance in Kilometers", comment: "")
default:
break
}
hourPickView.accessibilityLabel = NSLocalizedString("Time selector, enter the hours used for the distance selected", comment: "") // DON'T WORK
}
}
3rd, the method used with the pickerview which work if you add one label and it is returned for all the pickerview in the view controller, however, I don't know how to fix here to add one label for each of them.
func pickerView(_ pickerView: UIPickerView, accessibilityLabelForComponent component: Int) -> String?{
hourPickView.accessibilityIdentifier = "Hour"
secPickView.accessibilityIdentifier = "Sec"
minPickView.accessibilityIdentifier = "Min"
switch component {
case 0:
print("Este es: \(component)")
if hourPickView.accessibilityIdentifier == "Hour" {
switch pickerView.accessibilityIdentifier {
case "Hour"?:
return "Box for hour"
case "Min"?:
return "Box for minute"
case "Sec"?:
return "Box for second"
default:
return "Doesn't work"
}
//return "try to check what happens"
} else {
return "doesn't work"
}
default:
print("\(component)")
return "either"
}
}
As I told you all, thanks for your help!!!

Create nested map from key in groovy

I'm relatively new to groovy and am using it in the context of a gradle build. So please don't be harsh if there is an easy out-of-the-box solution for this.
Basically I'm trying to accomplish the reverse of Return Nested Key in Groovy. That is, I have some keys read from the System.properties map for example user.home and corresponding values like C:\User\dpr. Now I want to create a map that reflects this structure to use it in a groovy.text.SimpleTemplateEngine as bindings:
[user : [home : 'C:\Users\dpr']]
The keys may define an arbitrary deep hierarchy. For example java.vm.specification.vendor=Oracle Corporation should become:
[java : [vm : [spec : [vendor : 'Oracle Corporation']]]]
Additionally there are properties with the same parents such as user.name=dpr and user.country=US:
[
user: [
name: 'dpr',
country: 'US'
]
]
Edit: While ConfigSlurper is really nice, it is somewhat too defensive with creating the nested maps as it stops nesting at the minimum depth of a certain key.
I currently ended up using this
def bindings = [:]
System.properties.sort().each {
def map = bindings
def split = it.key.split("\\.")
for (int i = 0; i < split.length; i++) {
def part = split[i];
// There is already a property value with the same parent
if (!(map instanceof Map)) {
println "Skipping property ${it.key}"
break;
}
if (!map.containsKey(part)) {
map[part] = [:]
}
if (i == split.length - 1) {
map[part] = it.value
} else {
map = map[part]
}
}
map = it.value
}
With this solution the properties file.encoding.pkg, java.vendor.url and java.vendor.url.bug are discarded, which is not nice but something I can cope with.
However the above code is not very groovyish.
You can use a ConfigSlurper :
def conf = new ConfigSlurper().parse(System.properties)
println conf.java.specification.version

Autolayout constraints in TextField of an outlineView

last night a have an autolayout issue. I was googling and try to find something similar in SO. Even the apple doc doesn't point me to the right direction. Maybe my search terms are completely wrong.
Maybe you guys can bring some light into my darkness.
I added a NSOutlineView in storyboard and added some constraints to the NSTableCellView. As you can see, i added a trailing space to Superview of 50:
My example code adds some foo's and bar's into the outlineView by identifyer:
func outlineView(outlineView: NSOutlineView, viewForTableColumn tableColumn: NSTableColumn?, item: AnyObject) -> NSView? {
if tableColumn?.identifier == "NAME_COLUMN" {
cell = outlineView.makeViewWithIdentifier("NAME_COLUMN", owner: self) as NSTableCellView
cell.textField!.stringValue = "foo"
cell.textField!.editable = true
cell.textField!.delegate = self
} else
if tableColumn?.identifier == "VALUE_COLUMN" {
cell = outlineView.makeViewWithIdentifier("VALUE_COLUMN", owner: self) as NSTableCellView
cell.textField!.stringValue = "bar"
cell.textField!.editable = true
cell.textField!.delegate = self
}
return cell
}
But the trailing space will not show up in my running application!
I even try to set the cell display:
cell.needsDisplay = true
cell.needsLayout = true
cell.needsUpdateConstraints = true
or - according to someone on the internet - add "requiresConstraintBasedLayout":
class func requiresConstraintBasedLayout() -> Bool {
return true
}
but all without luck. The trailing space do not appears and the bar's on the right side border looks awful.
How do i use a TableViewCell inside a OutlineView with a trailing space?
Thanks a lot for any kind of hint.
ps

How to pre-select an option in a dropdown knockout js

I've looked at this other question, but can't get my select box to work correctly:
Binding initial/default value of dropdown (select) list
I've got the following Game object:
function Game(visitingTeamDetails, homeTeamDetails, game) {
if (arguments.length > 0) {
this.VisitingTeamDetails = visitingTeamDetails;
this.HomeTeamDetails = homeTeamDetails;
this.GameId = ko.observable(game.GameId);
this.HomeTeamName = ko.observable(game.HomeTeamName);
this.VisitingTeamName = ko.observable(game.VisitingTeamName);
this.SportTypeName = ko.observable(game.SportTypeName);
this.HomeAccountName = ko.observable(game.HomeAccountName);
this.VisitingAccountName = ko.observable(game.VisitingAccountName);
this.GameDateString = ko.observable(game.GameDateString);
this.GameTimeString = ko.observable(game.GameTimeString);
this.AvailableSportTypes = ko.observableArray(game.Sports);
this.sportTypeFunction = function () {
for (sportType in this.AvailableSportTypes()) {
if (this.AvailableSportTypes()[sportType].Name == this.SportTypeName()) {
return this.AvailableSportTypes()[sportType];
}
}
return null;
};
this.SportType = ko.observable(game.SportType);
}
}
SportType is an object with Name and SportTypeId.
I have the following template:
<td rowspan="3"><select data-bind="options: AvailableSportTypes, value: SportType, optionsText:'Name', optionsCaption: 'Choose...'" class="sportType"></select></td>
AvailableSportTypes is a list of SportType.
The list is coming in with the names of the SportTypes in the drop down list, but I can't make the initial selection be SportType. I wrote sportTypeFunction to show myself that the data was coming in correctly, and it would select the correct value, but changing my selection in the drop down would not update SportType.
I'm sure I'm doing something wrong. Anyone see it?
Thanks
When game.SportType gets passed in, it needs to be a reference to the an item in the game.AvailableSportTypes and not just an object that looks the same.
Basically two objects are not equal unless they are actually a reference to the same object.
var a = { name: "test" },
b = { name: "test" };
alert(a === b); //false
So, you would need to call your function to locate the correct object in the array and set it as the value of your observable.
Not that it is way better, but in KO 1.3 you can extend .fn of observables, observableArrays, and dependentObservables to add additional functionality.
Here is a sample: http://jsfiddle.net/rniemeyer/ZP79w

Reflection on a Scala case class

I'm trying to write a trait (in Scala 2.8) that can be mixed in to a case class, allowing its fields to be inspected at runtime, for a particular debugging purpose. I want to get them back in the order that they were declared in the source file, and I'd like to omit any other fields inside the case class. For example:
trait CaseClassReflector extends Product {
def getFields: List[(String, Any)] = {
var fieldValueToName: Map[Any, String] = Map()
for (field <- getClass.getDeclaredFields) {
field.setAccessible(true)
fieldValueToName += (field.get(this) -> field.getName)
}
productIterator.toList map { value => fieldValueToName(value) -> value }
}
}
case class Colour(red: Int, green: Int, blue: Int) extends CaseClassReflector {
val other: Int = 42
}
scala> val c = Colour(234, 123, 23)
c: Colour = Colour(234,123,23)
scala> val fields = c.getFields
fields: List[(String, Any)] = List((red,234), (green,123), (blue,23))
The above implementation is clearly flawed because it guesses the relationship between a field's position in the Product and its name by equality of the value on those field, so that the following, say, will not work:
Colour(0, 0, 0).getFields
Is there any way this can be implemented?
Look in trunk and you'll find this. Listen to the comment, this is not supported: but since I also needed those names...
/** private[scala] so nobody gets the idea this is a supported interface.
*/
private[scala] def caseParamNames(path: String): Option[List[String]] = {
val (outer, inner) = (path indexOf '$') match {
case -1 => (path, "")
case x => (path take x, path drop (x + 1))
}
for {
clazz <- getSystemLoader.tryToLoadClass[AnyRef](outer)
ssig <- ScalaSigParser.parse(clazz)
}
yield {
val f: PartialFunction[Symbol, List[String]] =
if (inner.isEmpty) {
case x: MethodSymbol if x.isCaseAccessor && (x.name endsWith " ") => List(x.name dropRight 1)
}
else {
case x: ClassSymbol if x.name == inner =>
val xs = x.children filter (child => child.isCaseAccessor && (child.name endsWith " "))
xs.toList map (_.name dropRight 1)
}
(ssig.symbols partialMap f).flatten toList
}
}
Here's a short and working version, based on the example above
trait CaseClassReflector extends Product {
def getFields = getClass.getDeclaredFields.map(field => {
field setAccessible true
field.getName -> field.get(this)
})
}
In every example I've seen the fields are in reverse order: the last item in the getFields array is the first one listed in the case class. If you use case classes "nicely", then you should just be able to map productElement(n) onto getDeclaredFields()( getDeclaredFields.length-n-1).
But this is rather dangerous, as I don't know of anything in the spec that insists that it must be that way, and if you override a val in the case class, it won't even appear in getDeclaredFields (it'll appear in the fields of that superclass).
You might change your code to assume things are this way, but check that the getter method with that name and the productIterator return the same value and throw an exception if they don't (which means that you don't actually know what corresponds to what).
You can also use the ProductCompletion from the interpreter package to get to attribute names and values of case classes:
import tools.nsc.interpreter.ProductCompletion
// get attribute names
new ProductCompletion(Colour(1, 2, 3)).caseNames
// returns: List(red, green, blue)
// get attribute values
new ProductCompletion(Colour(1, 2, 3)).caseFields
Edit: hints by roland and virtualeyes
It is necessary to include the scalap library which is part of the scala-lang collection.
Thanks for your hints, roland and virtualeyes.

Resources