i am trying add an additionalButton in inputToolbar of the JSQMessageViewController , but the problem is whenever inputToolbar resign or become firstResponder the frames of inputToolbar's contentView reFrames itself and additionalButton appears over the textview. can anyone help me what do in this situation by which the additionalButton and inputToolbar's textview not overlaps each other.
this is the following code:
#viewDidAppear
CGRect leftBtnFrame = self.inputToolbar.contentView.leftBarButtonContainerView.frame;
btnRabbit = [[UIButton alloc]initWithFrame:CGRectMake(leftBtnFrame.origin.x + leftBtnFrame.size.width +5 , leftBtnFrame.origin.y, leftBtnFrame.size.width, leftBtnFrame.size.height)];
[btnRabbit addTarget:self action:#selector(btnRabbitPressed) forControlEvents:UIControlEventTouchUpInside];
[btnRabbit setImage:[UIImage imageNamed:#"donkey_icon"] forState:UIControlStateNormal];
textViewFrame = self.inputToolbar.contentView.textView.frame;
textViewFrame.origin.x = textViewFrame.origin.x +btnRabbit.frame.origin.x - self.inputToolbar.contentView.leftContentPadding;
textViewFrame.origin.y = textViewFrame.origin.y;
textViewFrame.size.height = textViewFrame.size.height;
textViewFrame.size.width = textViewFrame.size.width - btnRabbit.frame.origin.x;
[self setToolBar];
#setToolBar
[UIView animateWithDuration:0.2 animations:^{
[self.inputToolbar.contentView addSubview:btnRabbit];
} completion:^(BOOL finished) {
[self.inputToolbar.contentView.textView setFrame:textViewFrame];
}];
and i am calling setToolBar in the following keyboard notification observer delegate
-(void)keyboardWillShow:(NSNotification *)notification {
[self setToolBar ];
}
]2]2]3
Here is how I've implemented
- (void)setupRightBarButtonItems {
UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.inputToolbar.contentView.rightBarButtonItem = sendButton;
sendButton.translatesAutoresizingMaskIntoConstraints = NO;
[sendButton setImage:[UIImage imageNamed:#"sendButtonActive"] forState:UIControlStateNormal];
[sendButton setImage:[UIImage imageNamed:#"sendButton"] forState:UIControlStateDisabled];
sendButton.contentMode = UIViewContentModeRight;
sendButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[sendButton setImageEdgeInsets:UIEdgeInsetsMake(0, -4, 0, 0)];
[sendButton removeConstraints:sendButton.constraints];
//Autolayout
[sendButton setTrailingConstant:0];
[sendButton setTopConstant:0];
[sendButton setBottomConstant:0];
[sendButton setWidthConstant:54];
self.sendButton = sendButton;
self.inputToolbar.contentView.rightBarButtonItemWidth = 114;
self.videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.videoButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.inputToolbar.contentView.rightBarButtonContainerView addSubview:self.videoButton];
[self.videoButton setImage:[UIImage imageNamed:#"videoButton"] forState:UIControlStateNormal];
self.videoButton.contentMode = UIViewContentModeRight;
self.videoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[self.videoButton addTarget:self
action:#selector(didTappedVideoButton:)
forControlEvents:UIControlEventTouchUpInside];
//Autolayout
[self.videoButton setHorizontalSpacing:0 fromView:self.sendButton];
[self.videoButton setTopConstant:0];
[self.videoButton setBottomConstant:0];
[self.videoButton setWidthConstant:30];
//
self.photoButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.photoButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.inputToolbar.contentView.rightBarButtonContainerView addSubview:self.photoButton];
[self.photoButton setImage:[UIImage imageNamed:#"photoButton"] forState:UIControlStateNormal];
self.photoButton.contentMode = UIViewContentModeRight;
self.photoButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[self.photoButton addTarget:self
action:#selector(didTappedPhotoButton:)
forControlEvents:UIControlEventTouchUpInside];
//Autolayout
[self.photoButton setHorizontalSpacing:0 fromView:self.videoButton];
[self.photoButton setTopConstant:0];
[self.photoButton setBottomConstant:0];
[self.photoButton setWidthConstant:30];
self.inputToolbar.contentView.rightContentPadding = 0;
}
And the result:
Setup your button
let btnRabbit = UIButton()
btnRabbit.setImage(#imageLiteral(resourceName: "Rabbit"), for: .normal)
place it in toolbar with this:
self.inputToolbar?.contentView?.leftBarButtonItem = btnRabbit
That will put it where you want and adjust the text input correctly.
Related
I am trying to have a checkbox using button and this is what I am doing:
int checkBoxWidth = foregroundHeight / 12.0;
int checkBoxHeight = foregroundHeight / 12.0;
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, checkBoxWidth, checkBoxHeight)];
//[checkBox setBackgroundImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
//[checkBox setBackgroundImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
[checkBox setImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
checkBox.layer.borderWidth = 1.0;
checkBox.layer.borderColor = [Utility primaryColor].CGColor;
checkBox.layer.cornerRadius = cornerRadius;
[switchView addSubview:checkBox];
but the image appears only when I touch the button, in any other state image is not showing up. Is there something missing?
You have to change the button state for the image to be changed. Use the below code to achieve your desired result.
int checkBoxWidth = foregroundHeight / 12.0;
int checkBoxHeight = foregroundHeight / 12.0;
UIButton* checkBox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, checkBoxWidth, checkBoxHeight)];
[checkBox setImage:[Utility getEmptyCheckBoxOutIcon] forState:UIControlStateNormal];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateHighlighted];
[checkBox setImage:[Utility getCheckBoxOutIcon] forState:UIControlStateSelected | UIControlStateHighlighted];
[checkBox addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
checkBox.layer.borderWidth = 1.0;
checkBox.layer.borderColor = [Utility primaryColor].CGColor;
checkBox.layer.cornerRadius = cornerRadius;
[switchView addSubview:checkBox];
And add this target function
-(IBAction)buttonClicked:(UIButton*)sender{
sender.selected = !sender.selected;
}
Use interactivePopGestureRecognizer when popViewController.
Set Custom back button and keep interactivePopGestureRecognizer = YES.
- (void)setNavigation {
[self.navigationController.scrollNavigationBar setNavigationTitleColor:[UIColor whiteColor]];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back"]
style:UIBarButtonItemStyleDone
target:self
action:#selector(popViewController)];
backButton.tintColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = backButton;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
}
have a likeButton on the ViewController.
- (YMFeedLikeButton *)likeButton {
if (!_likeButton) {
YMFeedLikeButton *likeButton = [[YMFeedLikeButton alloc] init];
replyButton.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, 150, 50);
[likeButton setImage:[UIImage imageNamed:#"topic-icon-like.png"]
forState:UIControlStateNormal];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-like.png"]
forState:UIControlStateNormal | UIControlStateHighlighted];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-liked.png"]
forState:UIControlStateSelected];
[likeButton setImage:[UIImage imageNamed:#"topic-icon-liked.png"]
forState:UIControlStateSelected|UIControlStateHighlighted];
UIImage *highlight = [UIImage imageNamed:#"highlight.png"];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted | UIControlStateSelected];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted | UIControlStateNormal];
[likeButton setBackgroundImage:highlight
forState:UIControlStateHighlighted];
[likeButton addTarget:self action:#selector(like)
forControlEvents:UIControlEventTouchUpInside];
[likeButton setTitleColor:[UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:1.0f]
forState:UIControlStateNormal];
[likeButton setTitleColor:[UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:1.0f]
forState:UIControlStateSelected];
[likeButton setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
likeButton.value = 0;
[self insertSubview:_likeButton = likeButton atIndex:0];
}
return _likeButton;
}
likeButton Highlighted is not working when i clicked it.
If close interactivePopGestureRecognizer
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
or change likeButton.frame = CGRectMake(150, 150, 150, 50);
,likeButton Highlighted touch is working.
i hope likeButton Highlighted is working when usedinteractivePopGestureRecognizer.
It's late, but anyway... This solution works for me:
self.navigationController.interactivePopGestureRecognizer.delegate = self;
self.navigationController.interactivePopGestureRecognizer.cancelsTouchesInView = NO;
self.navigationController.interactivePopGestureRecognizer.delaysTouchesBegan = NO;
self.navigationController.interactivePopGestureRecognizer.delaysTouchesEnded = NO;
Im getting touch location coordinates from UIWebView and displays the button with coordinates. It's working with coordinates match. So, i need to increase the button. I didn't use Array for button. When i delete particular button from UIWebView it delete all the button. Shall i use NSMutableArray?. Here x & y is the touch point coordinates in float value
if(x && y){
NSLog(#"x is %f",x);
NSLog(#"y is %f",y);
button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
action:#selector(click1:)
forControlEvents:UIControlEventTouchDown];
[button1 setTitle:#"click" forState:UIControlStateNormal];
button1.frame = CGRectMake(x, y, 30.0, 20.0);
// [btnArray addObject:button1];
button1.tag = gTag;
gTag++;
[wbCont.scrollView addSubview:button1];
}
Delete the button:
-(void)recycle:(id)sender{
for(int i=1;i<gTag;i++){
[[wbCont.scrollView viewWithTag:i] removeFromSuperview];
}
-(void)click1:(id)sender{
UIButton *mainBtn = (UIButton *)sender;
int mainTag = mainBtn.tag;
txtview = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,568)];
txtview.font = [UIFont fontWithName:#"Helvetica" size:12];
txtview.font = [UIFont boldSystemFontOfSize:12];
txtview.backgroundColor = [UIColor whiteColor];
txtview.scrollEnabled = YES;
txtview.pagingEnabled = YES;
txtview.editable = YES;
txtview.tag = mainTag*1000;
for(int i=0; i<[textArray count];i++){
txtview.text=[textArray objectAtIndex:i];
}
[self.view addSubview:txtview];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Done" forState:UIControlStateNormal];
button.frame = CGRectMake(240.0, 20.0, 60.0, 40.0);
[txtview addSubview:button];
recyclebtn=[UIButton buttonWithType:UIButtonTypeCustom];
recyclebtn.tag = mainBtn.tag;
[recyclebtn addTarget:self action:#selector(recycle:) forControlEvents:UIControlEventTouchDown];
[recyclebtn setImage:[UIImage imageNamed:#"recycle.png"] forState:UIControlStateNormal];
recyclebtn.frame=CGRectMake(0, 10, 30, 30);
[txtview addSubview:recyclebtn];
}
-(void)recycle:(id)sender {
UIButton *btnTemp = (UIButton *)sender;
[[wbCont.scrollView viewWithTag:btnTemp.tag] removeFromSuperview];
int txtTag = btnTemp.tag*1000;
[[self.view viewWithTag: txtTag] removeFromSuperview];
}
I have about 10-12 buttons that I'm adding to my scrollview. How can I make these into an array of buttons so that I can simplify the code? As of right now my code(only first three buttons are shown) is as follows:
UIButton *redButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
redButton.frame = CGRectMake(0, 0, 50, 30);
redButton.tag = 2;
[redButton setTitle:#"red" forState:UIControlStateNormal];
redButton.backgroundColor = [UIColor redColor];
[redButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[redButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:redButton];
UIButton *blueButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
blueButton.frame = CGRectMake(70, 0, 50, 30);
blueButton.tag = 3;
[blueButton setTitle:#"blue" forState:UIControlStateNormal];
blueButton.backgroundColor = [UIColor blueColor];
[blueButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[blueButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:blueButton];
UIButton *greenButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
greenButton.frame = CGRectMake(140, 0, 50, 30);
greenButton.tag = 5 ;
[greenButton setTitle:#"green" forState:UIControlStateNormal];
greenButton.backgroundColor = [UIColor greenColor];
[greenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[greenButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.scollView addSubview:greenButton];
...
Can you see if this is possible
- (void)addButtonsToScrollView
{
NSArray *buttons = #[#{#"Tag":#2,#"Title":#"red",#"Color":[UIColor redColor]},
#{#"Tag":#3,#"Title":#"blue",#"Color":[UIColor blueColor]},
#{#"Tag":#5,#"Title":#"green",#"Color":[UIColor greenColor]}];
CGRect frame = CGRectMake(0.0f, 0.0f, 50.0f, 30.0f);
for (NSDictionary *dict in buttons)
{
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
button.tag = [dict[#"Tag"] integerValue];
[button setTitle:dict[#"Title"]
forState:UIControlStateNormal];
button.backgroundColor = dict[#"Color"];
[button setTitleColor:[UIColor blackColor]
forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
frame.origin.x+=frame.size.width+20.0f;
}
CGSize contentSize = self.scrollView.frame.size;
contentSize.width = frame.origin.x;
self.scrollView.contentSize = contentSize;
}
You can use:
[NSArray arrayWithObjects:redButton,greenButton,blueButton,nil];
But might be better off using a NSDictionary
[NSDictionary dictionaryWithObjectsAndKeys:
redButton, #"red",
blueButton, #"blue",
greenButton, #"green",
nil];
That way you can use the key to look them up instead of index.
Ok, we have the solution, try this code mate,
-(void) createButtons{
NSDictionary *buttonColors = #{#"Red":[UIColor redColor],#"Green":[UIColor greenColor],#"Black":[UIColor blackColor],#"Yellow":[UIColor yellowColor],#"Blue":[UIColor blueColor]};
int tag = 1;
for(NSString *key in buttonColors.allKeys){
UIColor *color = [buttonColors objectForKey:key];
UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect frame = CGRectMake(((tag -1)*70), 0, 50, 30);
[button setFrame:frame];
button.tag = tag;
button.backgroundColor = color;
[button setTitleColor:color forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:key forState:UIControlStateNormal];
[self.scrollView addSubview:button];
tag++;
}
}
I have a UIView Subclass that has a frame/image with buttons in it. I'm trying to add it to my view controller however when I do it shows up but none of the buttons will register ANY touches.
Here is my UIView Subclass.
#import "ControlView.h"
#implementation ControlView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect slideFrame = CGRectMake(0, 402, 320, 82);
slider = [[UIView alloc] initWithFrame:slideFrame];
slider.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"shelf.png"]];
[self addSubview:slider];
UIImage *btnImage = [UIImage imageNamed:#"NavBar_01.png"];
UIImage *btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btnImage = [UIImage imageNamed:#"NavBar_01.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_01_s.png"];
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(12, 28, 70, 50);
[btn1 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn1 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn1];
[slider bringSubviewToFront:btn1];
[btn1 addTarget:self action:#selector(home) forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_02.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_02_s.png"];
btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame = CGRectMake(87, 28, 70, 50);
[btn2 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn2 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn2];
[slider bringSubviewToFront:btn2];
// [btn2 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_03.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_03_s.png"];
btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(162, 28, 70, 50);
[btn3 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn3 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn3];
[slider bringSubviewToFront:btn3];
// [btn3 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
btnImage = [UIImage imageNamed:#"NavBar_04.png"];
btnImageSelected = [UIImage imageNamed:#"NavBar_04_s.png"];
btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
btn4.frame = CGRectMake(237, 28, 70, 50);
[btn4 setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn4 setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[slider addSubview:btn4];
[slider bringSubviewToFront:btn4];
// [btn4 addTarget:self action:#selector() forControlEvents:UIControlEventTouchUpInside];
UISwipeGestureRecognizer *recognizer;
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideUp)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
[self addGestureRecognizer:recognizer];
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(slideDown)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
[self addGestureRecognizer:recognizer];
}
return self;
}
-(void)slideUp
{
// Slide up based on y axis
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 320;
slider.frame = frame;
[UIView commitAnimations];
}
-(void)slideDown
{
CGRect frame = slider.frame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.75];
frame.origin.y = 425;
slider.frame = frame;
[UIView commitAnimations];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
And this is how I'm adding it to my ViewController.
ControlView *cont = [[ControlView alloc]init];
[homeView.view addSubview:cont];
I tried adding the same code to a ViewController and the buttons etc.. worked so I'm sure this is something simple/stupid with Delegates/Self etc.. between the subview and the ViewController however I have a mental block.
You are doing this?
ControlView *cont = [[ControlView alloc]init];
you are supposed to do
ControlView *cont = [[ControlView alloc]initWithFrame:self.view.frame];