UIbutton image changes to default - ios

I have a Uibutton and its default background images is set i changes its image at run time but i want to preserve the selected image. but each time i click on uibutton image changes to default. Here is the code
- (void)changeColorOnRuntTime:(UIColor *)color{
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:color] forState:UIControlStateNormal];
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:color] forState:UIControlStateHighlighted];
// [btnTmpForColorPicker seti];
// tmpColor = [UIColor alloc]ini;
tmpColor = [color copy];
// NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
// [defaults setValue:color forKey:#"CustomPreColor"];
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:color] forKey:#"CustomPreColor"];
}
and in btnHandler
if (popOverColorPicker.popoverVisible) {
[popOverColorPicker dismissPopoverAnimated:YES];
}
else
{
sdViewController = [[SDColorPickerViewController alloc]init];
sdViewController.color = [UIColor whiteColor];
sdViewController.tag = 1;
sdViewController.delegate = self;
// CGRect rect = CGRectMake(viewController.view.frame.origin.x, viewController.view.frame.origin.y, viewController.view.frame.size.width, 380);
// viewController.view.frame = rect;
btnTmpForColorPicker = (UIButton*)sender;
UIButton *senderbt = (UIButton*) sender;
if (tmpColor) {
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:tmpColor] forState:UIControlStateNormal];
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:tmpColor] forState:UIControlStateHighlighted];
}
// [popOverColorPicker setPopoverContentSize:CGSizeMake(viewController.view.frame.size.width, 380)];
popOverColorPicker = [[UIPopoverController alloc]initWithContentViewController:sdViewController];
[popOverColorPicker setPopoverContentSize:CGSizeMake(300, 390)];
[popOverColorPicker presentPopoverFromRect:senderbt.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

Replace this:
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:color] forState:UIControlStateHighlighted];
with this:
[btnTmpForColorPicker setBackgroundImage:[self imageNamed:#"customColor.png" withTint:color] forState:UIControlStateHighlighted] || UIControlStateSelected];
And apply the same change to the btnHandler button

Related

Adding View/Buttons on JSQMessageViewController's inputToolbar

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.

UIButton set title color not working

I have created the array of UIButtons , having the same action,
if i click the first button, the first button Colour should be change, other button Colour should not be changed. how to achieve this?
for(titleStr in titleArray){
actionButton = [UIButton buttonWithType:UIButtonTypeCustom];
[actionButton addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
actionButton.tag = count;
[actionButton setTitle:titleArray[count] forState:UIControlStateNormal];
[actionButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[actionButton setBackgroundColor:[UIColor clearColor]];//[UIColor greenColor]]; // AJS 3.3 change
CGSize expectedTitleSize = [actionButton.titleLabel sizeThatFits:maximumLabelSize];
CGRect buttonframe;
buttonframe=CGRectMake(contentOffset,0, expectedTitleSize.width+5.0, expectedTitleSize.height+25);
actionButton.frame = buttonframe;
[titlewidthArray addObject:[NSNumber numberWithFloat:expectedTitleSize.width]];
[contentoffsetArray addObject:[NSNumber numberWithFloat:contentOffset+3.0]];
if(count==0){
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
[actionButton setSelected:YES];
tempObj = actionButton;
}else {
actionButton.titleLabel.font=[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0];
}
[titleScrollView addSubview:actionButton];
contentOffset += actionButton.frame.size.width+5.0;
titleScrollView.contentSize = CGSizeMake(contentOffset, titleScrollView.frame.size.height);
// Increase the count value
count++;
}
-(void)buttonTapped:(id)sender {
if([sender tag]==0){
UIButton *tappedButton = (UIButton *)sender;
[actionButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
actionButton.titleLabel.textColor = [UIColor whiteColor];
[actionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
[tappedButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
Thanks advance
for that you have to take one extra UIButton Object in you header file.
While user click on any button you have to store that selected Button in refbutton object and use like below.
#interface YourViewController ()
{
UIButton *btnSelected;
}
Now Move to your Button Action:
-(IBAction)buttonTapped:(UIButton *)sender {
if(_btnSelected){
_btnSelected.titleLabel.textColor = [UIColor blackColor];
}
_btnSelected = sender;
sender.titleLabel.textColor = [UIColor whiteColor];
}
Hope this will help you.

button Highlighted not work when interactivePopGestureRecognizer.enabled = YES

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;

enable a segment controller view on View load

i have a segment controller, now and on its tap on segment 1 , a view appears and on tap of second segment another view is show, just below this segment controller. now i want when the view on which this segment controller is present appears, the 1st segment controller view appears with that and the first segment controller button appears to be tapped, in advance.
i am able to get this thing done by showing my view in this manner. but i am doin unecessary thing.. please advise me some other way.
-(void)loadTest
{
if(!cabinInfoDetailsViewObj)
{
cabinInfoDetailsViewObj = [[CabinInfoDetailsView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[self addSubview:cabinInfoDetailsViewObj];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_WhiteButton.png"] forSegmentAtIndex:0];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_GreyButton.png"] forSegmentAtIndex:1];
}
this is segment controller part
- (IBAction)CabinInfoSegmentButtonTapped:(id)sender
{
if(CabinInfoPopupSegmentControl.selectedSegmentIndex == 0)
{
if(!cabinInfoDetailsViewObj)
{
cabinInfoDetailsViewObj = [[CabinInfoDetailsView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_WhiteButton.png"] forSegmentAtIndex:0];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_GreyButton.png"] forSegmentAtIndex:1];
cabinInfoDetails.textColor = [UIColor blueColor];
cabinInfoLocate.textColor =[UIColor blackColor];
[cabinInfoDeckPlansViewObj removeFromSuperview];
[self addSubview:cabinInfoDetailsViewObj];
}
if(CabinInfoPopupSegmentControl.selectedSegmentIndex == 1)
{
if(!cabinInfoDeckPlansViewObj)
{
cabinInfoDeckPlansViewObj = [[CabinInfoDeckPlansView alloc]initWithFrame:CGRectMake(232, 188, 617,412)];
}
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_WhiteButton.png"] forSegmentAtIndex:1];
[CabinInfoPopupSegmentControl setImage:[UIImage imageNamed:#"16.4_GreyButton.png"] forSegmentAtIndex:0];
cabinInfoDetails.textColor = [UIColor blackColor];
cabinInfoLocate.textColor =[UIColor blueColor];
[cabinInfoDetailsViewObj removeFromSuperview];
[self addSubview:cabinInfoDeckPlansViewObj];
}
in my application,used this code :
//Give .h file UISegmentedControl *seg;
- void)viewDidLoad
{
seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Notification", #"Messages", nil]];
seg.tag = 1;
seg.segmentedControlStyle = UISegmentedControlStyleBar;
seg.frame = CGRectMake(1, 0, 318, 45);
[seg addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
seg.tintColor = [UIColor whiteColor];
seg.selectedSegmentIndex = 0;
seg.backgroundColor=[UIColor clearColor];
[seg setDividerImage:[UIImage imageNamed:#""] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[seg setImage:[UIImage imageNamed:#"notificationselected.png"] forSegmentAtIndex:0];
[seg setImage:[UIImage imageNamed:#"message.png"] forSegmentAtIndex:1];
[self.view addSubview:seg];
}
pragma mark - UISegmentedControl
- (IBAction)segmentAction:(id)sender
{
UISegmentedControl *segControll = (UISegmentedControl *)sender;
if (segControll.selectedSegmentIndex==0)
{
[segControll setImage:[UIImage imageNamed:#"notificationselected.png"] forSegmentAtIndex:0];
[segControll setImage:[UIImage imageNamed:#"message.png"] forSegmentAtIndex:1];
//right code for select the segmentcontroll.index=0
}
else if (segControll.selectedSegmentIndex==1)
{
[segControll setImage:[UIImage imageNamed:#"notification.png"] forSegmentAtIndex:0];
[segControll setImage:[UIImage imageNamed:#"messageselected.png"] forSegmentAtIndex:1];
//right code for select the segmentcontroll.index=1
}
}
i hope it will help you.

Trying to place UILabel on top of UIButton in a custom UIAlertView

What I'm trying to do is place a UILabel on top of a UIButton.
The UIButton have an image as a background and the result is I cannot see the normal text on the UIButton, therefore I think to place the UILabel on top of it.
My code is:
NAlertView *customAlert = [[NAlertView alloc] initWithTitle:#"title" message:#"message" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:#"cancel", nil];
[customAlert show];
NAlertView.m:
- (void)layoutSubviews
{
for (id obj in self.subviews) //Search in all sub views
{
if ([obj isKindOfClass:[UIImageView class]]) // Override UIImageView (Background)
{
UIImageView *imageView = obj;
[imageView setImage:[UIImage imageNamed:#"CustomAlertView"]];
[imageView setAlpha:0.7];
[imageView sizeToFit];
}
if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
{
UILabel *label = (UILabel*)obj;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor clearColor];
}
if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
{
UIButton *button = (UIButton*)obj;
CGRect buttonFrame = button.frame; // Change UIButton size
buttonFrame.size = CGSizeMake(127, 35);
CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
buttonFrame.origin.y = newYPos;
button.frame = buttonFrame;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
switch (button.tag)
{
case kFIRST_BUTTON:
{
[button setImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"short_button_gold_on.png"] forState:UIControlStateHighlighted];
}
break;
case kSECOND_BUTTON:
{
[button setImage:[UIImage imageNamed:#"short_button_black_off.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"short_button_black_on.png"] forState:UIControlStateHighlighted];
}
break;
}
}
}
[self updateConstraints];
}
Result:
That means you just want to add Title on UIButton?
Try This -
[button setBackgroundImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
[button setTitle:#"Title" forState:UIControlStateNormal];
Replace
[button setImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
to
[button setBackgroundImage:[UIImage imageNamed:#"short_button_gold_off.png"] forState:UIControlStateNormal];
change all such possibilities.
and use the below method to change the text of the button
[button setTitle:#"text" forState:UIControlStateNormal];
Hope it helps.

Resources