I'm trying to make UIMenuController to display copy&paste menu when the UITextView got long pressed. It works if there is only one UITextView on the screen. However, if there are multiple UITextViews on the screen, long press on the active UITextView (The one is the firstResponder) triggers magnifying glass to be shown, when finger is released, the copy&paste menu is displayed, however, the menu is dismissed automatically in less than one second.
The following is the view hierarchy:
UIView -> UIImageView -> MyUIView -> UITextView
|__> MyUIView -> UITextView
I set userInteractionEnabled and editable of non-active UITextViews to NO. Any idea on how to make the copy&paste menu stay?
I am using Xcode 4.6.3 and test it on iPhone 4S with iOS 7.0
Apparently, whenever there's a change in firstResponder, UIMenuController gets dismissed!
In order to track the source of the change, if other, you can listen to UIMenuControllerWillHideMenuNotification and check who's dismissing the menu.
I found a solution. I didn't have enough details in the question, that's probably why there was no answers.
The key missing piece is that I subclassed UITextView and it overrides layoutSubviews (see buggy margin behavior with dynamically resizing UITextView). The solution is simply removing layoutSubviews function, and I used krafter's suggestion to address UITextView cut off issue.
Related
TL;DR
Is it a new feature in Xcode 14 and iOS 16 that a UIScrollView is now scrolled automatically when a view inside the ScrollView becomes firstReponder and the keyboard appears? While working on other iOS projects in earlier versions of Xcode and iOS I never came across this behaviour. Can this be controlled or deactivated?
Details:
While working with Xcode 14.0.1 on a new iOS project I noticed that an UIScrollView is now automatically scrolled when the content view becomes first responder.
Have a look at this example. I have created a new project, added a ScrollView to the ViewController and placed 12 subviews inside to fill it with some content. Subview no. 12 additionally holds a UITextField. The firstResponder status of this text field can be toggled using the top button.
When tapping on the button the TextField becomes firstResponder and the ScrollView is automatically scrolled up. However, not enough to make bring the TextField above the keyboard and make visible. Just enough to bring it above the bottom screen edge.
This does not make a lot of sense to me.
Is it possible to control this automatic scrolling or to deactivate it?
EDIT:
I have created CustomScrollView as UIScrollView subclass and did override scrollRectToVisible. When calling super.scrollRectToVisible everything works as before, but I could set a break point to see from where scrollRectToVisible is called.
The callstack shows the method scrollTextFieldToVisibleIfNecessary inside UITextField which uses delegateShouldScrollToVisibleWhenBecomingFirstResponder
However, UITextFileDelegate does not have such a method / value and scrollTextFieldToVisibleIfNecessary is also documented nowhere.
So, the question remains: How to disable this behaviour?
I have an issue with Catalyst. I'm trying to display a long text in a UITextView, and on iOS everything works well, no issues.
On Mac (with Catalyst) however, when I scroll the textview, some parts disappear. Does anyone have an idea on how to fix this?
I have tried calling setNeedsLayout/Display/layoutIfNeeded on the textview when scrolling but no luck. The only thing that "worked" is to remove the textview from the view hierarchy and readd it to the container view on scroll finished. Still not ideal, as you lose selection when you want to select some text and scroll. (When I save the selection and re-apply it, then the blue highlight is invisible)
Using a UILabel in a scrollview could work, but then you lose all user interactions (no editing/selecting text)
Does anyone have any idea on how to fix this? Or I should just wait for Apple to fix this.
See the video/gif of the original issue:
Whenever I tap on my UITextView on my testing device, the text is pushed far to the right as demonstrated in the gif below.
I can't see why this behaviour is happening, within my Storyboard I have tried to disable scrolling and have applied constraints to ensure that the width of the UITextView is only ever the width of the device, yet this still happens. The UITextView has not been subclassed or had any additional attributes applied to it via code.
If anybody has had this problem before it would be great to get some insight around how I could remedy it.
EDIT Constraints for the text view.
I have a TextView made in the Interface Builder;
It scrolls fine with its default text, but as soon as I programatically set the text to something else, it no longer scrolls.
I do not disable scrolling, and I have tried enabling scrolling (setScrollEnabled:YES), and user interaction (setUserInteractionEnabled:YES) both of which make no difference.
The things that I have found:
It scrolls with the default text showing
It doesn't seem to matter what text I set in it, it just stops scrolling as soon as it is set. I'm doing it programatically like this:
self.myTextView.text = #"any text"; (or [self.myTextView setText:#"any text"]; has the same effect.)
I have two other Textfield in the same View, one of which is using the View Controller as its delegate. The TextField in question is not using the View Controller as its delegate. None of the delegate methods set anything to do with scrolling.
Both of the other TextViews have their scrolling capability set to defaults. If I change the one that doesn't need it to no scrolling, it makes no difference to the problem.
The one that has the View Controller as its delegate continues to scroll after I edit it (non programatically) and this one continues to be able to scroll once the one in question freezes.
If I delete these two other TextFields, then the one in question works as expected, i.e. it scrolls after adding text.
Any thoughts?
Thanks.
Make sure your new text is long enough to be able to get scroll.
if text is just one line it wont scroll but if its larger then text area size then it will scroll its by default settings
I've began iOS development recently and have created a basic layout using Interface Builder. However, I've noticed that my UIScrollView is causing my UITextField and UIButton to be unclickable (up to a certain point).
Here is a screenshot. Everything above the red line is clickable and responds to touch events, but everything below is/does not.
I decided to move my UIScrollView down a few pixels, and noticed this "red line" moved down as well. After shrinking the height of the UIScrollView from 589px to 550px, I am now able to click anywhere on the button/in the text field (since the theoretical "red line" is now just below the button). Here is that screenshot:
So, my question is, why was the UIScrollView doing this to my UITextField and UIButton? It seems as though it was overlapping the text field and button somehow, despite it appearing to be physically below them. Is there any way to avoid this "overlap" without having to sacrifice those 39 pixels I gave up to prevent the "overlap"?
Write this code in viewDidLoad function
[self.view sendSubviewToBack:scrolView];
Hope this helps you..