Vertical UICollectionView to UIButton focus in tvOS - uibutton

I have a UICollectionView that scrolls vertically with about 15 cells. Then directly below this UICollectionView I have a UIButton. I can successfully focus the UIButton, but only if I scroll down through all of the UICollectionViewCells. I am wondering if it is possible to transition focus from the first UICollectionViewCell down to the UIButton without having to scroll through all of the UICollectionView's items?
A diagram of the desired behavior:

If you really want that behavior then you can return the button in the following function,
override var preferredFocusEnvironments: [UIFocusEnvironment]
And then subclass the following function,
override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
and return false when the focus is about to go to second cell and before returning call setNeedsFocusUpdate() which will make system check for preferred focus element and moves the focus to the button.
Really wonder why you are after this behavior 🤔, but its doable.

Did you try with canFocusAtItem to all other collectionView cells to false.
Then directly the focus will go the UIButton.

Related

avoid the NSImageView change the image after drag & drop

I have coded drag & drop for the NSImageView, and I set the NSImageView as the PDF/image files drop zone. But I found once the files dropped, the NSImageView shows the dropped file content and image. How can I avoid that? I want NSImageView shows the default image I set when the application launch. I also tried to add this code but doesn't work:
[imgView setImage:[NSImage imageNamed:#"default.jpg"]];
after you process data, you can clear NSDraggingInfo like this:
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
// process your data form NSDraggingInfo
// and clearContents
sender.draggingPasteboard().clearContents()
return true
}
Create a subclass of ImageView. Remove the drawRect:(NSRect)rect method if there is in its implementation file. You won't need it.
Click on the identity inspector tab of the sidebar to the right. Then enter the name of your imageView subclass after selecting an imageView control, which you have already done, probably.
Click on the attributes inspector tab of the sidebar. Select your imageView control and then enter the name of the background image in the top combobox.
Subclass NSImageView and implement following method.
Setting new image with draggingInfo is done from concludeDragOperation by default.
override func concludeDragOperation(_ sender: NSDraggingInfo?) {
return
}

UITableView EditingStyle clear button hides

I'm currently creating my TableView, but I'm finding some problems with the EditingStyle and a TextField in the UITableViewCell.
So basically, the UITableView is set to EnableEditing for true at the beginning, so users can easily delete the dynamic content they are giving in the UITextFields in my UITableViewCells.
But the problem now is, by enabling the editing mode from my TableView the Clear button of the Textfield goes to the right. Is there a way that i can solve this?
The second picture is from my Storyboard. I've aligned the UITextField right to the Cell.
You can subclass the UITextField Class and override this method
- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x - 30, bounds.origin.y, bounds.size.width, bounds.size.height);
}
Good luck
Used AutoLayout for making the constraints. Worked well now

NSStepper in NSTableCellView has no effect

I’m using a regular (not subclassed) NSTableCellView in a view-based table view. It has the initial image and text field views. I added an NSStepper to the view.
The text field is bound to tableCellView.objectValue.quantity.
The stepper’s value is bound to tableCellView.objectValue.quantity too.
The problem is that when running the app, when I click the stepper it doesn’t seem to get the mouse event, neither arrow gets highlighted, the value is not incremented or decremented.
If I set the double action of the table view it gets triggered if I double-click the stepper as if it was transparent.
What am I missing?
Thanks!
You should look at the documentation but easiest is that you need to subclass NSTableView and override this method to validate the proposed first responder. As the document states NSTableViews disallow some controls to be used unless the row is first selected. Even then it still may discard some.
- (BOOL)validateProposedFirstResponder:(NSResponder *)responder forEvent:(NSEvent *)event {
return YES;
}
Further to the correct answer from Robert Payne, with Swift you could add an extension to NSTableView and not subclass it.
extension NSTableView {
override public func validateProposedFirstResponder(responder: NSResponder, forEvent event: NSEvent?) -> Bool {
return true
}
}
And I'd like to emphasis that it's the NSTableView not the NSTableViewCell.

Don't change NSButton appearance when pressed or disabled

I have two NSButtons with images for both their on states and off states. Only one should be active at a time; click one and then click the other to change a property back and forth.
The problem is that if I disable a button when it's clicked so it cannot be clicked again, then the image is dimmed when the button is disabled--and I don't want it dimmed, I just want to use the alternate image.
On the other hand, if I just leave the button enabled, but programmatically just don't run any code when it is clicked, then there's a flashing effect as the mouse clicks--which is distracting, when the button should not do anything.
So I either need to prevent the button from being dimmed when it is disabled, or prevent it from changing the button appearance while the mouse button is held down.
After reading up, it sounds like I need to subclass NSButtonCell and override - (BOOL)imageDimsWhenDisabled to do the former. But I can't figure out exactly how to subclass it (what sort of NSButtonCell class I should inherit from) and if the "setCell" method of NSButton is enough to use the new NSButtonCell class, or if I need to subclass NSButton as well.
Some tips on that would be appreciated, or perhaps there's a completely different approach that would achieve my objectives.
Check this out:
[btnInfo.cell setImageDimsWhenDisabled:NO];
When you want to disable it without changing appearance do this:
On MacOS - NSButton:
Only option is to subclass NSButton and override mouseDown function
class RadioButton: NSButton {
override func mouseDown(with event: NSEvent) {}
}
On iOS - UIButton:
Simple disable UserInteraction
mybutton.isUserInteractionEnabled = false
For a more up to date answer in Swift, this works for me:
(theButton.cell! as! NSButtonCell).imageDimsWhenDisabled = false

touchesBegan in UIView not being called

I know that this question must have been answered plenty of times already, but I have no idea why the touchesBegan: is not called in my UIView.
Before I added it in my app, I made a trial project to do some tests. What I did was subclass a UIView where the touchesBegan: is implemented and add this as a subview of another view. The superview has a button which shows the subclassed UIView once clicked. Everything works as expected.
However, when I did the same thing in my existing app, the touchesBegan: method, as well as the touchesMoved:, were never called. Can anyone explain what could be preventing the methods from being called?
I just found the answer to this. I realised that the superview of the UIView has the userInteraction set to disabled (which is the default value). So when I enabled the userInteraction of the superview, the touches are now recognised.
In my case background = UIColor.clearColor() was the reason why touchesBegan was not called. Its obviously not called on transparent elements.
I was struggling on this problem for a while... and i figure it out. I think that the touchesBegan override function listen on the main view: the first one on the storyboard tree of the ViewController! I would love to post an image of my storyboard to be more clear, but i can't! ;)
Anyway, IF you have subviews, they may covers the main view... in this manner the touch wont "reach" the main view.
To avoid this you'll have to set the Background property in the Attribute Inspector to Default for ALL THE SUBVIEWS. In this way the background will be transparent and the touch will be able to reach the main view.
If you don't want to set the background to transparent there is something you can do:
-Add an outlet of your last view (with background set)
-Add a tap gesture recognizer to it
-Handle the tap disposing the keyboard
ES.
In properties:
//The last view's outlet
#IBOutlet weak var ContainerView: UIView!
In viewDidLoad():
let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
self.ContainerView.addGestureRecognizer(tap)
Outside:
func handleTap(recognizer: UITapGestureRecognizer){
self.view.endEditing(true)
}