how to enable to move cursor in UITextView - ios

I create UItextView in pragmatically, and when I tap to move cursor in UITextView, it does not worked. But it worked using IB. Do I supposed to set some attributes to UItextView or UIView?
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0,320, 460)];
textView.userInteractionEnabled = YES;
textView.editable = YES;
textView.multipleTouchEnabled =YES;
[self.view addSubview:textView];
textView.delegate = self;
I found default long tap does not working.

I have problem with add subView for gesture recognizer inside custom cell.
When I blocked it or move to background it've started to work:
contentView.insertSubview(view, atIndex: 0)
where view is view with gesture recognizer.

Related

How to I add a button on top of iCarousel?

I added the date label and the share button via code. Now I want to add a static button in the right top corner to open a slide out menu(I'm using MMDrawerController). If I'm adding the via code, it's moving with the carousel.
If I'm adding a button on the name label(My App) on the storyboard, it's not getting clicked and the carousel gesture is being registered instead of the button click.
How should I proceed to add a button for a slide out menu on the right ?
-(UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
UIView *magView = nil;
CGFloat height = self.myCarousel.bounds.size.height;
CGFloat width = self.myCarousel.bounds.size.width;
//Magazine View
magView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, width/1.35, height/1.75)];
magView.backgroundColor = [UIColor clearColor];
UIImageView *magImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"4 1:2.jpg"]];
magImage.frame = CGRectMake(0, 0, width/1.35, height/1.75);
[magView addSubview:magImage];
//Date Label
UILabel *dateLabel = nil;
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, height/1.7, width/4, height/25)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.text = #"12-03-2017";
dateLabel.font = [UIFont fontWithName:#"Roboto-Black" size:5];
dateLabel.textColor = [UIColor whiteColor];
//Share Button
UIButton *shareButton = nil;
shareButton = [[UIButton alloc] initWithFrame:CGRectMake(width/1.49, height/1.7, height/25, height/25)];
shareButton.backgroundColor = [UIColor clearColor];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_icon_1x"] forState:UIControlStateNormal];
[magView addSubview:dateLabel];
[magView addSubview:shareButton];
return magView;
}
Simulator
Storyboard
How do I proceed to add the button ?
Add button to storyboard and after adding carousel programmatically. add code to bring the button to top on view.
[self.view bringSubviewToFront:button];
if you are adding it programmatically then just after [self.view addSubview:carousel];
call [self.view bringSubviewToFront:button](make sure button is added on view before calling it)
I solved the issue. Instead of taking the view of the ViewController I dragged/dropped another view onto the ViewController and displayed the carousel on it. Now I can set the button easily. Thanks for the help though. Cheers
It will help you
[xyzButton.superview setUserInteractionEnabled:YES];
:)

UIButton not Responsive When placed out of its container UIScrollView

I have a UIScrollView. Inside this UIScrollView is a UIView which contains every other child views (including a UIButton). It has the same bounds as the UIScrollView.
I want:
The bottom of the UIScrollView is not scroll-able (where the UIButton is placed).
The UIButton is responsive.
So I set the bottom of the UIScrollView a little higher than the UIVIew as below image:
This will make the UIButton not responsive (it's responsive in the left layout). Any suggestion? Thanks!
Some part of my code:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.scrollView];
self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero];
[self.scrollView addSubview:self.scrollContent];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.scrollContent addSubview:self.button];
[self.button addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
Update:
Looks like the right way is to keep the bottom of the scroll view and set its contentSize, but since I'm using autolayout, I found that the height of the contentSize is always zero???
You need to place the button in a view that is outside of the scroll views view hierarchy.
Currently the hit testing is failing because the button is outside of the scroll views bounds but within its hierarchy.
Add the button into scroll view after that try the below code:
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
[self addSubview:self.scrollView];
self.scrollContent = [[UIView alloc] initWithFrame:CGRectZero];
[self.scrollView addSubview:self.scrollContent];
self.scrollView.delaysContentTouches = NO; //Add this line
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
[self.scrollContent addSubview:self.button];
[self.button addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];

How To Fix UITextView in storyboard TextView

Here i want to fix a UITextView which i created programatically in textView added on storyboard. I have tried it this way but not working.
_textView is a textView in which i have to fix UITextView *descrip
contentView is the view on which i have _textView.
UITextView *descrip = [[UITextView alloc]init];
descrip.view.frame = CGRectMake(_textView.frame.origin.x,_textView.frame.origin.y,_textView.frame.size.width,_textView.frame.size.height);
[self.contentView addSubview:descrip.view];
You can do something like this--
UITextView *descrip = [[UITextView alloc]initWithFrame:_textView.frame];
[self.contentView addSubview:descrip];
there is no need to to use CGRectMake if you want to give same frame as _textView frame.
here is proper method
UITextView *descrip = [[UITextView alloc]initWithframe:CGRectMake(_textView.frame.origin.x,_textView.frame.origin.y,_textView.frame.size.width,_textView.frame.size.height)]
[self.contentView addSubview:descrip];
If I understood correctly, you are trying to add descrip into_textView; try this:
UITextView *descrip =[[UITextView alloc]initWithFrame:CGRectMake(0, 0, _textView.frame.size.width, _textView.frame.size.height)];
descrip.backgroundColor=[UIColor blueColor];
[self.contentView addSubview:descrip]; //here add subview to texview not content view

UITapGestureRecognizer on UILabels in subview of UIScrollView not working

I have a problem where my UITapGestureRecognizer on my UILabels in a content view in my UIScrollView is not calling it's methods.
The view hierarchy is as follows:
scrollView (UIScrollView)
contentView (UIView)
testLabel (UILabel) - here is where the UITapGestureRecognizer is attached
I have distilled the code down to an example to highlight the problem
// Set scrollview size - Added in Storyboad
[scrollView setContentSize:CGSizeMake([arrayOfVerbs count]*self.view.frame.size.width, scrollView.contentSize.height)];
[scrollView setCanCancelContentTouches:YES]; // Tried both yes and no
[scrollView setPagingEnabled:YES];
// Add content view
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];
[scrollView addSubview:contentView];
// Add test UILabel
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[testLabel setBackgroundColor:[UIColor redColor]];
[testLabel setText:#"Test touch"];
[testLabel setUserInteractionEnabled:YES];
[contentView addSubview:testLabel];
// Add gesture recogniser
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(playSound:)];
singleTap.numberOfTapsRequired = 1;
[testLabel addGestureRecognizer:singleTap];
And this is the method that the tap gesture recogniser should call
- (void)playSound:(UITapGestureRecognizer *)sender {
NSLog(#"play sound");
if(sender.state == UIGestureRecognizerStateEnded)
{
int pronounNumber = [sender.view tag];
int exampleNumber = (int)sender.view.frame.origin.x%(int)self.view.frame.size.width;
NSLog(#"Pronoun is %i and example is %i", pronounNumber, exampleNumber);
}
}
This method is never called when I tried to touch on the UILabel.
I have tried setting the property canCancelContentTouches to both YES and NO on the scroll view as suggested by this thread, but it's still not working.
The strange thing is, if I add a UILabel outside of the scrollView, then the gesture recogniser works! So the problem only occurs in my contentView which is a subview of my scrollView.
I am using auto-layout, if that might be any difference?
Thanks!
The scroll view also has a gesture recogniser. By default, only 1 gesture recognizer can be handling touches at any one time. You need to make yourself the delegate of your gesture and then implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: to return YES. This will allow it to work at the same time as the scroll view.
Add delegate to tagGestures,
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(playSound:)];
singleTap.numberOfTapsRequired = 1;
singleTap.delegate = self;
[testLabel addGestureRecognizer:singleTap];
EDIT:-
contentView.userInteractionEnabled = YES;
put this line your code it'l work.
[yourlabel.addGestureRecognizer:tapGestureDeFromage];
should add the gesture explicitly to your labels.

ios uiview uiimageview uitextview

I have a main UIView that has an UIImageView on it, and the UIView adds an UITextView as a subview. UITextView's frame is smaller than UIView. UIImageView is actually a frame and the rest of the image is transparent and fits the full size of the UIView, but the UITextView is always upon UIView. What I want to achieve is UITextView shows UIView in foreground, while it is possible to scroll up and down on UITextView.
What is the proper way to make it possible?
My current code that does not work:
in loadView:
self.contentview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.contentview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"ChristmasFrame.png"]];
self.contentview.opaque = NO;
self.contentview.userInteractionEnabled = YES;
[self setView:self.contentview];
in viewDidLoad:
[self.contentview addSubview:self.textview];
[self.contentview sendSubviewToBack: self.textview];
I want UITextView to be behind self.contentview, so it could show its background image upon the UITextView.
try setting setUserInteractionEnabled: to NO on UIView what is on top of UITextView
if that not working you could try to override hitTest:withEvent:
make clear color as textview's background color.
and add text view above UIView

Resources