I have two UILabels in a UIStackView. Is it possible for the UIStackView to get focus and gestures in tvOS?
Items in a stack view can receive focus, but not the stack view itself. By default, labels cannot receive focus either. Buttons are the most commonly items that receive focus.
You can make a stack view focusable by subclassing the stackview, overriding canBecomeFocused and using that subclass:
class FocusableStackView: UIStackView {
override var canBecomeFocused: Bool {
return true
}
}
Related
I have a segmented control in my application where one of the tabs is a chat controller.
I'm using the input accessory view for the toolbar that has the send button and the textfield.
The problem is i can't hide the input accessory view while switching tabs
I tried hiding it, removing it and adjusting the collection view bottom constraint accordingly when it's hidden or not but that didn't work
override var inputAccessoryView: UIView
{
return inputContainerView
}
override func canBecomeFirstResponder() -> Bool {
return true
}
I have a UIStackView, created in Interface Builder that contains two views. Now I'm trying to programatically change the order of these views (exchange their positions, so to speak).
Is there an easy fix for this that DOES NOT use Interface Builder to accomplish this?
Thanks :).
Create a layout with 2 UIViews in a UIStackView like so:
Create an outlet for your UIStackView to your UIViewController. Call addArrangedSubview on your UIStackView in viewDidLoad to change the order. See snippet below.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var stackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
// Switches order of the stackView
stackView.addArrangedSubview(self.stackView.subviews[0])
}
}
This should be the result:
The addArrangedSubview takes the given view and adds it to the end of the arrangedSubviews array. In our case we take the red view at index[0] and add it on the end (right) of the UIStackView.
You can change the view semantic of UIStackView from Unspecified to Force Right-to-Left to swap the position of of nested view.
you can use addArragnedSubview to reorder the views.
Only addArragnedSubview is needed because when adding the view to a new parent it is removed from the old parent.
if self.viewsSwitched{
stackview.addArrangedSubview(self.stackview.subviews[0])
self.viewsSwitched = false
}else{
stackview.addArrangedSubview(self.stackview.subviews[1])
self.viewsSwitched = true
}
This code exchanges the two views inside the stackview by each call
I subclassed a UIViewController to present it as a pop over on iPhone using its popoverPresentationController.
The view controller's view contains only a UILabel that is added using auto layout constraints.
The presented pop over is much larger than the label. I want the pop over to be only as large as necessary to fit its content. I tried by overriding preferredContentSize but that didn't work:
override var preferredContentSize: CGSize {
get {
label.sizeToFit()
view.sizeToFit()
return view.bounds.size
}
set {}
}
How can I make sure the pop over automatically resizes to its content?
I stuck with scrolling content inside UIScrollView in my tvOS app.
I have scrollView with height = 400 and width = 400. Inside this scrollview I have non-scrollable UITextView with height = 800 and width = 400. So I want to scroll this text.
But my scroll view is not scrolled, I don't see any scrolling indicators and also my scrollView have isFocused value = false. How can I solve this problem?
p.s. update I created separate ViewController (image below).
It have black ScrollView and white view with big height with label in the middle of it. ScrollView has fixed width and height and there is no scrolling for some reason! I even didn't connect any separate class - just created it from Interface builder.
UIView isn't focusable by default (and as such in tvOS it can't be scrolled) . You have to subclass it to override canBecomeFocused:
class myUIView: UIView {
override var canBecomeFocused: Bool {
return true
}
}
Then in your example use the class myUIView instead of UIView for your long white view. Setting the right constraints you wouldn't need to design the views out of the controller view boundaries in IB either. Check this answer with the linked gist to see how to build the constraints.
I think you can use the
func gestureRecognizer (_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
method to judge the view and scroll it
According to the view you touch to do something to intercept
There is some way to fix your problem.
Please check your scrollview in storyboard, is your scrolling or vertical indicator is checked?
U can make your textview scrollable if u just want to scroll the textview content
There is problem in your constraint, you need to cleary add the height, vertical space top, vertical space bottom from your textview to your scrollview.
Hope these advice can help you
So you have to set the scrollview's content size larger than the scrollview bounds, so you should set contentsize to (400, 800), probaly in you xib
I am using AVPlayerController and I have added a subview on top of AVPlayerController. As Soon the subview is displayed I want to shift focus from the player to the Subview and focus the UIButton added on top of that Subivew. I have tried preferredfocusview but its a read-only property and I cannot change it. Can anyone pelase assist. Thanks a lot.
First of all, preferredFocusView is deprecated so you should use preferredFocusEnvironments instead.
It is a computed property, which means you don't assign to it, you override it:
override var preferredFocusEnvironments: [UIFocusEnvironment] {
//if subview exists, return the subview or the button, whichever is focusable
return [subview]
//otherwise use the default implementation of AVPlayerController
return super.preferredFocusEnvironments
}
You should also call self.setNeedsFocusUpdate() and self.updateFocusIfNeeded() to request a focus update when you add the subview.