How to hide input accessory view? - swift

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
}

Related

Swift 5 Segmented Control Truncates the Titles

I am using dynamic segmented control. Whenever user adds new decks, the title is showing up in the segmented control.
However, I need a horizontally scrollable segmented controller that might have different title width depending on the text (title) size.
this helped me to achieve it. iOS 13 Segmented Control: Remove swipe gesture to select segment
I added my segmented controller inside of scroll view.
Then changed my segmented controller class to the following;
class NoSwipeSegmentedControl: UISegmentedControl {
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

how to hide textfield and auto layout the constraint in swift

so i have a segmented control in my project.I want to hide and auto layout my button.But the problem if i just hide the textfields and labels the button under my segmented control still not moving.So there is a space between my segmented control and next button.I want to move my next button up if i click no on my segmented control.
-if i click yes on segmented control
The next button under my segmented control will constraint my textfield and show all textfields and labels under my segmented control
-if i click no on segmented control
The next button under my segmented control will constraint to my segmented control and hide all textfields and labels under my segmented control
thanks for all the answers
the image
A quick example of how you could change the position of the button depending on the segment switch.
Below image shows dragging the auto layout height between Segment switch and button
Below Image shows using outlet and type needs to be NSLayoutConstraint
In your ViewController.Swift the code should look something like below.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var switchControl: UISegmentedControl!
#IBOutlet weak var heighConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func switchPressed(_ sender: Any) {
switch switchControl.selectedSegmentIndex
{
case 0:
// here you can see the constant is being set, this will determine the
// position of your button. You will need to set the correct position.
heighConstraint.constant = 150
case 1:
heighConstraint.constant = 200
default:
break;
}
}
}
Hope this answer helps.

Hide navigation bar with table view

I'm trying to hide navigation bar when I scroll my table view. It seems that when I scroll inside table view this one can't detect the scrolling event.. when I scroll outside table view navigation bar get hidden...how can I resolve ?
You need to set the navigation controller hideBarsOnSwipe to true
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.hidesBarsOnSwipe = true
}

NSSearchField and NSSegmentedControl inside an NSToolbar

The setup:
I'm writing an document-based app targeting OS X 10.11 using storyboards. The main window has an NSToolbar with a 3-segment NSSegmentedControl. When the segmented control is clicked, it should toggle the collapsed state of an NSSplitViewItem in a horizontal or vertical NSSplitView. The behavior I'm trying to achieve is the same as in Xcode 7 where a segmented control in the toolbar shows/hides the Navigator/Debug Area/Utilities views.
Currently the segmented control sends an action to the first responder. The action method is implemented by an NSSplitViewController subclass, that then toggles it's NSSplitViewItem's collapsed state.
The problem:
The issue is that the toolbar also contains an NSSearchField. If the NSSearchField has focus, or even if the segmented control itself has focus, clicking on the NSSegmentedControl with the cursor does not result in the action method correctly making it's way up the responder chain to the NSSplitViewController subclass.
Attempted solutions:
Previously I worked around this issue using notifications instead of target/action, but it ended up being too convoluted in the end. Another idea is to send the message to the window controller, which would then pass it to it's content view controller, which would pass it to the vertical split view controller that would then send the message (if needed) to the horizontal split view controller. While I know this would work, it also seemed like an ugly solution having to add code to 2 additional files that simply passed a message along, I thought this was what using the responder chain avoided.
Any insights would be grealy appreciated.
Final solution:
I realized that wiring the segmented control's action up to the first responder only makes sense if the key view context is important. In this case the segmented control should toggle the collapsed state of split view items in multiple nested split views, regardless of what the key view is.
Define an enumeration to represent areas of the split view:
enum SplitViewArea : Int {
// The raw values must match the order of the segmented control
case left, top, right
}
Define a protocol to communicate that a split view area should be toggled:
protocol SplitViewTogglable {
func toggleSplitViewItem(matching area: SplitViewArea)
}
Implement the segmented control action method in the window controller:
#IBAction func segmentedControlSelectionStateDidChange(_ sender: Any) {
guard let segmentedControl = sender as? NSSegmentedControl else { return }
guard let area = SplitViewArea(rawValue: segmentedControl.selectedSegment) else { return }
guard let togglable = contentViewController as? SplitViewTogglable else { return }
togglable.toggleSplitViewItem(matching: area)
}
Implement the SplitViewTogglable protocol's method in the NSSplitViewController subclass:
func toggleSplitViewItem(matching area: SplitViewArea) {
switch area {
case .left:
leftSplitViewItem.isCollapsed = !leftSplitViewItem.isCollapsed
case .top:
// Nested NSSplitViewController that adopts SplitViewTogglable
if let togglable = centerSplitViewItem.viewController as? SplitViewTogglable {
togglable.toggleSplitViewItem(matching: area)
}
case .right:
rightSplitViewItem.isCollapsed = !rightSplitViewItem.isCollapsed
}
}
Is the NSSplitViewController set as the contentViewController of the window?
As part of the responder chain search for an action target, the window will consider its contentViewController as a supplemental target if it responds to the action selector.
When the search field has key focus the responder chain does not go through the normal content area, and instead goes through the toolbar to the window. So the only way the NSSplitViewController could be a part of that search is to be the contentViewController.

In iOS - When touching a text field in a scroll view the view can not scroll

When trying to scroll a view by touching and dragging on a text field, similarly to how you would scroll around on the edit contacts view on the iPhone, the view will not move, touching and dragging in between the text fields or off to the side of the text fields in the scroll view works but not when touching a textfield.
Any insights would be greatly appreciated. Thanks!
Try setting the delaysContentTouches property to YES for the UIScrollView to which you have added to text field. This should allow you to scroll if you touch and then drag within a text field.
Setting delaysContentTouches to true didn't work for me either. What did was iterating through the scrollview's gesture recognizers and setting each of their delaysTouchesBegan and cancelsTouchesInView to true and false, respectively:
scrollView.gestureRecognizers?.forEach {
$0.delaysTouchesBegan = true; $0.cancelsTouchesInView = false
}
I think it solution in this situation
extension UITableView {
override public func touchesShouldCancelInContentView(view: UIView) -> Bool {
if view is UITextField || view is UITextView {
return true
}
return super.touchesShouldCancelInContentView(view)
}
}
try with below approach. Just replace your UIScrollView with ScrollView and adds the code to your project.
SWIFT 4.1
class ScrollView: UIScrollView {
override func touchesShouldCancel(in view: UIView) -> Bool {
if type(of: view) == TextField.self || type(of: view) == UITextView.self {
return true
}
return super.touchesShouldCancel(in: view)
}
}