add button inside a text view - ios

I have a text view in which i want a button for camera. In my application i want that user type some text then he/she want to attach a photo or video. I have done that but the camera button i want inside the text view. when i try to add a button as subview that button scrolled with the text but i want button fixed and another problem is that the text comes in button area not visible so i want that the cursor will not go in that area which is covered by button.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(0, 0, 50.0, 30.0);
[editButton addTarget:self action:#selector(editPressed:)forControlEvents:UIControlEventTouchUpInside];
myButton.backgroundColor=[UIColor blueColor];
[txtView addSubview:myButton];

-
Try this .It will definitely work for you and will solve your issue
Write below code in ViewDidLoad Method.
.
-(void)textResizeForButton{
CGRect buttonFrame = self.button.frame;
UIBezierPath *exclusivePath = [UIBezierPath bezierPathWithRect:buttonFrame];
self.textView.textContainer.exclusionPaths = #[exclusivePath];
}

Add it on self.view not on UITextField so it won't be scrolled anywhere. Just get the CGrect right.
For offsetting the text on the right to fit UIButton try this:
UIView *rightMarginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, leftMargin, txtField.frame.size.height)];
rightMarginView.userInteractionEnabled = NO;
[txtField setRightView:rightMarginView];
txtField.rightViewMode = UITextFieldViewModeAlways;

Related

Is it possible to dim a UIView but allow touch of elements behind it?

How would you have a grey overlay on top, but allow to touch buttons beneath it?
You can set userInteraction of overlay to false.
As mentioned, you can set userInteractionEnabled to NO.
But pay attention: when you create a full screen transparent view, and set its userInteractionEnabled to NO, all the subviews of that view will not be responsive to user actions. If you add a button on your view, it will not respond to user taps! and another problem: if you set the alpha value of that view to be transparent, e.g. 0.4, then all its subviews will be transparent too!
The solution is simple: don't put any subview on your transparent view, but instead, add your other elements as siblings of your view. Here is Objective-C code to show what I mean: (notice the comments which say it all):
//Create a full screen transparent view:
UIView *vw = [[UIView alloc] initWithFrame:self.view.bounds];
vw.backgroundColor = [UIColor greenColor];
vw.alpha = 0.4;
vw.userInteractionEnabled = NO;
//Create a button to appear on top of the transparent view:
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 80, 44)];
btn.backgroundColor = [UIColor redColor];
[btn setTitle:#"Test" forState:UIControlStateNormal];
[btn setTitle:#"Pressed" forState:UIControlStateHighlighted];
//(*) Bad idea: [vw addSubview:btn];
//(**) Correct way to have the button respond to user interaction:
// Add it as a sibling of the full screen view
[self.view addSubview:vw];
[self.view addSubview:btn];

View with subviews in a uinavigationbar and uitoolbar

I really need your help with this one (first post on SO -- be gentle):
I have two dynamic UIButtons which I would like to have centered in a UIView, which in turn should be centered in a UINavigationbar and UIToolbar. I can't - despite a lot of Googling - seem to figure out a proper way to do this.
This is what I've done so far:
In viewDidLoad, I add the two buttons as subviews and set the view as the UINavigationbar's titleView
self.myClass.viewForTitleAndButton = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 32)];
[self.myClass.viewForTitleAndButton setBackgroundColor:[UIColor blackColor]];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myButton];
[self.myClass.viewForTitleAndButton addSubview:self.myClass.myOtherButton];
self.navigationItem.titleView = self.myClass.viewForTitleAndButton;
In a method being triggered when I press certain buttons, I set the title (and bounds) of one of the buttons depending on what's clicked:
CGSize titleSize = [title sizeWithAttributes:#{NSFontAttributeName : [UIFont italicSystemFontOfSize:17.0]}];
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGFloat newX = (screenSize.width - titleSize.width) / 2;
CGRect buttonFrame = self.myClass.myButton.frame;
//Removing the line below doesn't do any difference at the moment
self.myClass.myButton.bounds = CGRectMake(newX, buttonFrame.origin.y, titleSize.width+8, buttonFrame.size.height);
[self.myClass.myButton setTitle:title forState:UIControlStateNormal];
NSLog(#"Title: %#", title);
//title is a NSString that changes depending on what is clicked. I am 100% sure it changes as I can see it in the log every time the method is triggered.
The problem is that the title of myButton is not changed. It worked before with the very same button when it was placed in a different spot and not as a subview.
Q1: What am I missing to make the title of the button change?
Q2: Is adding the buttons as subViews to a view that is then placed in the navigationbar and toolbar respectively a sound way to accomplish what I want?
This is really bugging me, any pointers in the right direction is much appreciated.
I don't think you can add a button which is already an outlet of a view controller. Thinking in another way, a button(view) can only has one superview.
Create button dynamically for the title view.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *iv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
iv.backgroundColor = [UIColor greenColor];
titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
titleButton.frame = CGRectMake(0, 0, 120, 44);
[titleButton setTitle:#"test" forState:UIControlStateNormal];
[titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[iv addSubview:titleButton];
self.navigationItem.titleView = iv;
}
//some event to change the button title
- (void)click:(UIButton *)button
{
[titleButton setTitle:#"I changed" forState:UIControlStateNormal];
}

Adding a close button to the modal view that is partially off the view

I am trying to add a "X" button to the top of the modal view so if the user presses that it closes the modal view. I looked at the solution proposed here how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?
But I have two follow up questions here as i am fairly new to ios programming
The solution proposed in the above page does not seem to work for me. I tried it with a normal button first in the viewDidLoad of the modal VC and I am seeing it appear only within the bounds of modal view and not outside as I want.
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
buttonView.backgroundColor = [UIColor brownColor];
CGRect buttonFrame = CGRectMake( 0, 0, 100, 50 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: #"Close" forState: UIControlStateNormal];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[button addTarget:self
action:#selector(closeButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
CGRect parentView = self.view.superview.frame;
CGRect currentView = self.view.frame;
CGRect buttonViewFrame = buttonView.frame;
buttonViewFrame.origin = CGPointMake(parentView.origin.x - buttonViewFrame.size.width/2.0f, parentView.origin.y - buttonViewFrame.size.height/2.0f);
[buttonView setFrame:buttonViewFrame];
[self.view addSubview:buttonView];
what I am seeing is
How do I draw the "X" button do I use CGRect to draw it our or instead of adding a button in the above example should I just add a image with the "X" in it as the subview?
thanks in advance for the help
do like this, hope helps u :)
//adding button to your view
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 300, 350)];
aView.backgroundColor = [UIColor blueColor];
aView.tag = 100;
[self.view addSubview:aView];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitle:#"CLOSE" forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = CGRectMake(aView.bounds.origin.x, aView.bounds.origin.y, 80, 30);// adjust with respect to bounds
aButton.backgroundColor = [UIColor redColor];
[aView addSubview:aButton];
[aView release]; //i am doing without ARC
The other option is to create a larger view and put your UITableView within this, along with your close button. The close button can then be at position (0,0) and the UITableView can start at (25,25).
This prevents complications - I have seen clipped views fail to recognise the touch outside of the main view, so your 50x50 button only has a touch area of 25x25.

Strange UIButton behaviour on iOS5

I create a UIButton and place it on top of the UIImageView which I place on UIScrollView. I add the button to the image like this:
backgroundImage.frame = CGRectMake(0, 0, backgroundImage.frame.size.width, mainTextView.frame.origin.y + mainTextView.frame.size.height + bottomMargin + attachBlockHeight);
[self insertSubview:backgroundImage atIndex:0];
UIButton *tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(0, imgGreyLine.frame.origin.y + imgGreyLine.frame.size.height, backgroundImage.frame.size.width, attachBlockHeight)];
[tmpButton addTarget:self action:#selector(btnAttachClicked) forControlEvents:(UIControlEventTouchUpInside)];
//tmpButton.backgroundColor = [UIColor redColor];
[backgroundImage addSubview:tmpButton];
And the button works great on iOS6 and up - it's there, it receives touch events and does what it has to do.
However, on iOS5 I encounter an enormously strange behaviour - the frame of the button stays exactly the same and the button is clearly present on the view, but it only receives touch events when I slide my finger left or right on it. Moreover, there are no other UIViews on top of the button anywhere near!
What can be the reason for it? I'm totally confused in finding the bug here.
EDIT: It turns out that sliding UIButton works on iOS6 as well, but the thing is that the touch events are also registered.
You need to enable user interaction on the UIImageView parent
eg:
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backgroundImage setBackgroundColor:[UIColor yellowColor]];
[backgroundImage setUserInteractionEnabled:YES];
[self.view addSubview:backgroundImage];
UIButton *tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,50,50)];
[tmpButton addTarget:self action:#selector(btnAttachClicked) forControlEvents:(UIControlEventTouchUpInside)];
tmpButton.backgroundColor = [UIColor redColor];
[backgroundImage addSubview:tmpButton];
You can not add UIbutton as a subview of UIImageview. Add it to the scrollview or the superview of UIimageview for that matter
I think the proper way to do what you want is to have a UIView with the imageview and button as siblings/children. make sure to disable user interaction on the imageview and you should be good.

Click on UIButton Does Not Work if it's Added as Subview to UIImageView

I have a simple UIImageView that is animated, and i need to detect a tap on it. I've added a UIButton that is a subview of this UIImageView. However,the tap on it does not work. I've already tested the UIButton itself when added to the view,and it works, so it's not the button that is causing the problem.
Here's the code:
self.red = [[UIImageView alloc] initWithFrame:CGRectMake(50, 300, 85, 100)];
self.red.image = [UIImage imageNamed:#"red.png"];
[self.red setUserInteractionEnabled: YES];
[self.view addSubview: self.red];
[self.view bringSubviewToFront:self.red];
UIButton *redButton = [UIButton buttonWithType: UIButtonTypeCustom];
redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height);
[redButton addTarget: self action:#selector(correctButtonClick) forControlEvents: UIControlEventAllTouchEvents];
[redButton setUserInteractionEnabled:YES];
[self.red addSubview: redButton];
[self.red bringSubviewToFront:redButton];
What am I doing wrong here?
Instead of this line , redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height); try this
redButton.frame = CGRectMake(0,0, self.red.frame.size.width, self.red.frame.size.height);
EDIT : This line [self.red bringSubviewToFront:redButton]; is no needed..
Instead of inserting a UIButton as a subview use a UITapGestureRecognizer, for example:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(correctButtonClick)];
[self.red addGestureRecognizer:tap];
What you have to do is to invert your implementation, adding UIButton to UIImageView is not a good idea, rather create a custom styled UIButton programatically, and add UIImageView as its subview, would definitely work
EDIT:
As for you current implementation, youve added UIButton inside a UIImageView so probably you want every part of image to accept the click event, in that case your x and y coords of UIButton would be 0,0 rather than red.x and red.y, however width and height are correct
Just do self.red.userInteractionEnabled = YES.
UIImageView has userInteractionEnabled NO as default.
The custom button has no UI. So you can't select it. (you can try it with bound button , or add some image to button and set button alpha to 0.1 or else)
#tkanzakic answer should be correct.
Just add TapGestureRecognizer to ImageView or add Image to Button.
Instead of using separate imageview and Button, make use of the imageview property within the Button. Which will solve all your problems and relieves you from writing extra lines of code.
UIButton *redButton = [UIButton buttonWithType: UIButtonTypeCustom];
redButton.imageView.image = [UIImage imageNamed:#"red.png"];
redButton.frame = CGRectMake(50, 300, 85, 100);
[redButton addTarget: self action:#selector(correctButtonClick) forControlEvents: UIControlEventAllTouchEvents];
[redButton setUserInteractionEnabled:YES];
[self.red addSubview: redButton];
It's simple, the button doesn't respond because its frame is wrong and so it's not where you think it is. Basically change your line below:
redButton.frame = CGRectMake(self.red.frame.origin.x, self.red.frame.origin.y, self.red.frame.size.width, self.red.frame.size.height);
with this:
redButton.frame = CGRectMake(self.red.bounds.origin.x, self.red.bounds.origin.y, self.red.frame.size.width, self.red.frame.size.height);

Resources