UIButton in TableViewCell VoiceOver Accessibility - ios

SOLVED:
I followed the link in the update and now the UITableViewCell's buttons are working for VoiceOver.
The Situation:
I'm implementing VoiceOver accessibility for an app.
I have a custom UITableViewCell (code below) that can toggle between an expanded and a normal visual (selected vs unselected) by the user clicking the cell. Everything involved with expansion works according to my design.
In the custom UITableViewCell, I have a UILabel and 2 UIButtons.
The UILabel is recognized by the VoiceOver accessibility feature, but the UIButtons are ignored.
The Problem:
I need the VoiceOver Accessibility feature to recognize the UIButtons and read their accessibility labels and/or hints.
The Code:
-(id) init{
...
_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_leftButton.titleLabel setFont:[UIFont fontWithName:FONT size:12]];
[_rightButton.titleLabel setFont:[UIFont fontWithName:FONT size:12]];
[_leftButton addTarget:self action:#selector(clickButtonLeft:) forControlEvents:UIControlEventTouchUpInside];
[_rightButton addTarget:self action:#selector(clickButtonRight:) forControlEvents:UIControlEventTouchUpInside];
[_leftButton setFrame:CGRectMake(CELL_DETAILS_SIDE_PADDING,
CELL_PADDING + CELL_NORMAL_HEIGHT + CELL_EXPANDED_PADDING - CELL_BUTTON_HEIGHT-7,
70, CELL_BUTTON_HEIGHT)];
[_leftButton setImage:[UIImage imageNamed:#"giftIcon.png"] forState:UIControlStateNormal];
[_leftButton setTitle:#" Gift This" forState:UIControlStateNormal];
[_rightButton setFrame:CGRectMake(self.frame.size.width - 140 - CELL_DETAILS_SIDE_PADDING,
CELL_PADDING + CELL_NORMAL_HEIGHT + CELL_EXPANDED_PADDING - CELL_BUTTON_HEIGHT-7,
140, CELL_BUTTON_HEIGHT)];
[_rightButton setImage:[UIImage imageNamed:#"buyPack.png"] forState:UIControlStateNormal];
[_rightButton setTitle:#" Buy For Yourself" forState:UIControlStateNormal];
_description = [[UILabel alloc] initWithFrame:CGRectMake(CELL_DETAILS_SIDE_PADDING, CELL_PADDING+CELL_NORMAL_HEIGHT+10,
self.frame.size.width - 2*CELL_DETAILS_SIDE_PADDING, 50)];
_description.numberOfLines = 0;
[_description setLineBreakMode:NSLineBreakByWordWrapping];
[_description setFont:[UIFont fontWithName:FONT size:13]];
[_description setBackgroundColor:[UIColor clearColor]];
[_description setTextColor:[UIColor whiteColor]];
}
- (void) expandCell
{
...
[self addSubview:_description];
[self addSubview:_leftButton];
[self addSubview:_rightButton];
}
UPDATE:
It looks like what is described here is the way to go.

This other stack overflow question has the answer. Follow the tutorial in the answers.
stack overflow answer source

Related

Creating a UIButton in a subclass of UIButton

I am trying to create a UIButton in a class that is a subclass of UIButton. The reason for this is that it would be easier for my app to do this. I am wondering if it is possible. I am able to create the button, but the action is not working. My selector exists, but is not getting called.
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, kThumbSide - 350, kThumbSide, 16)];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button2 setTitle:[NSString stringWithFormat:#"%#", [data objectForKey:#"username"]] forState:UIControlStateNormal];
[button2 sizeToFit];
button2.backgroundColor = [UIColor clearColor];
button2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button2.font = [UIFont fontWithName:#"Arial" size:15];
[button2 addTarget:delegate action:#selector(showUserViews) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button2];
Hey you are creating an Object of UIButton class not any Class clear???
and your target method are getting wrong Delegate.
try it
[button2 addTarget:self action:#selector(showUserViews) forControlEvents:UIControlEventTouchUpInside];
replace delegate with self for getting showUserViews method in same class
I haven't see any UIButton subclass here, perhaps you've asked your question wrong.
Any way you should do this:
- (void) viewDidLoad {
[super viewDidLoad]
UIButton *b = [[UIButton alloc]initWithFrame:YOUR_DESIRED_FRAME];
[b setBackgroundColor:[UIColor redColor]]
[b addTarget:self action:#selector(bPressed:) forControlEvents:UIControlEventTouchUpInside]
}
- (void)bPressed:(UIButton *)sender {
NSLog(#"b button pressed")
}

ios 8 - scroll does not work

I am using UIScrollView . At run time I am adding UIButton into scrollview. In iOS 7 it work perfectly but in iOS 8 I am getting problem on scrolling (it is very hard to scroll )..
Here is my code . Please suggest me What am I doing wrong or in iOS 8 is there any new property? .
-(void)renderAllCatealogue:(NSArray *)catalogueArr
{
[[scollViewCatlog subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
int x=0;
for (NSDictionary *dict in catalogueArr)
{
UIView *view =[[UIView alloc]initWithFrame:CGRectMake(x, 8, 1, 16)];
[view setBackgroundColor:[UIColor whiteColor]];
[scollViewCatlog addSubview: view];
//calculate width
UIFont *font=[UIFont fontWithName:BOLD_FONT size:15];
CGFloat width=[[CommonUISetting sharedInstance]widthOfString:[dict valueForKey:#"catalogTitle"] withFont:font];
UIButton * btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(x+10, 1, width, 30)];
[btn setTitle:[dict valueForKey:#"catalogTitle"] forState:UIControlStateNormal];
[btn setTag:[[dict valueForKey:#"catalogId"] integerValue]];
[btn.titleLabel setFont:font];
[btn addTarget:self action:#selector(catlogBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn.titleLabel setFont:[UIFont fontWithName:BOLD_FONT size:15]];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[scollViewCatlog addSubview:btn];
x=x+width +15;
}
[scollViewCatlog setContentSize:CGSizeMake(x, 1)];
}
i got solution .
I create a subclass of UIScrollview and add this method in it.
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}
I think you should use UICollectionView in order to generate something you want. UICollectionView allows to create vertical as well as horizontal infinite scroll panel. It is similar as the UITableView, you just have to setup some DataSource Methods and Delegate Methods.
look here for sample of UICollectionView

Switched from UILabel to UIButton. Text disappears

I just switched from a UILabel to UIButton, intending to use the button's titleLabel property to display the information formerly displayed in the label replaced by the button.
Problem is, the text doesn't show up. I've checked to see if the button itself (normally with a transparent background, changed to red to check) is appearing, and it is, but the text isn't there. Here's a sample of the relevant code, which is identical to the original (working) code from the label, changing only lines like:
UILabel *firstLabel;
(...)
firstLabel.textColor = [UIColor blackColor];
to:
UIButton *firstLabel;
(...)
firstLabel.titleLabel.textColor = [UIColor blackColor];
Here's a full chunk for clarity:
firstLabel.frame = CGRectMake(thisRiser.bounds.origin.x, thisRiser.frame.size.height -focusBarEndHeight - 55, barWidth, 60);
firstLabel.backgroundColor = [UIColor clearColor];
firstLabel.titleLabel.textColor = [UIColor blackColor];
firstLabel.titleLabel.text = [NSString stringWithFormat:#"%#\n%.2f%%\nTime--%#",focusItemName,focusItemPercent,actualDurationFocusItem];
[firstLabel.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
firstLabel.titleLabel.numberOfLines = 0;
firstLabel.titleLabel.textAlignment = NSTextAlignmentCenter;
[firstLabel setHidden:NO];
What am I missing? Any ideas?
Thanks!
UPDATE --
This may be related to a known bug!
Many thanks to the responders below. I implemented the first recommended fix, which resolved my initial problem, so my text now appears in the label. However, the UIControlStateHighlighted and UIControlStateSelected don't work.
For example, this code produces no discernible effect to the text when clicked:
firstLabel.backgroundColor = [UIColor clearColor];
[firstLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
After searching around SO for a while, I'm coming to the conclusion that there is a bug (at least in iOS 7) that prevents proper functioning of these methods. Unfortunately, I'm not smart enough to provide more detailed information, but plenty of recent posts point to a major problem in this area of UIControl.
I'll be ecstatic to be proven wrong, because I'd like to use this functionality.
UIButton responds to different messages, you can use the button state to change its title.
You can set the title like this;
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setTitle:#"Title goes here" forState:UIControlStateNormal];
See Control State: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIControl_Class/Reference/Reference.html#//apple_ref/c/tdef/UIControlState
To set a title in an UIButton, you have to init the button:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 60, 20)];
and then use:
[button setTitle:#"text" forState:UIControlStateNormal];
where you must specific the control state (highlighted, selected, etc.)
try for example,
UIButton *firstLabel = [UIButton buttonWithType:UIButtonTypeCustom];
firstLabel.frame = CGRectMake(0, 60, 100, 50); //set the frame
[firstLabel setTitle:#"Hello" forState:UIControlStateNormal];
[firstLabel setBackgroundColor:[UIColor redColor]];//for test
[firstLabel setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; //it will always displays green for normal
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];//if u touch it text changed to white instantly
[firstLabel setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];//if touch it and hold it shows white color

Custom UIButton in UIscrollview

I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.

Using custom UITableViewCell's accessory view to show two items

I've been looking for this for a while, but haven't found an answer (blame poor googling skills). I have a custom UITableViewCell class which, currently, consists of a custom UISwitch and a UILabel. I want to add a button that's only visible (And active) when the switch is set to "Yes". Right now I add the switch to the accessoryView, and leave it. However, the accessory view doesn't really have subviews, as far as I can tell, so here's my question:
Should I just create a UIView that has a button and a switch, size it to fit the cell's accessory view (or will it auto-size itself?), and put that in as the cell's accessory view? And is this typically the way that it's gone about?
Or is there a solution that I'm missing?
Thanks.
Here is an example:
UIButton* btdel = [[UIButton alloc] init];
btdel.tag = indexPath.row;
//[btdel setTitle:#"Delete Event" forState:UIControlStateNormal];
[btdel setBackgroundImage:[UIImage imageNamed:#"ButtonRemove.png"] forState:UIControlStateNormal];
[btdel addTarget:self action:#selector(deleteEvent:) forControlEvents:UIControlEventTouchUpInside];
// bt.titleLabel.frame = CGRectMake(0, 0, 95,24);
btdel.frame = CGRectMake(110, 0, 30,30);
[headerView addSubview:btdel];
[btdel release];
UIButton* bt = [[UIButton alloc] init];
bt.tag = indexPath.row;
[bt setTitle:#"Select a Dress" forState:UIControlStateNormal];
[bt setBackgroundImage:[UIImage imageNamed:#"findDress.png"] forState:UIControlStateNormal];
[bt addTarget:self action:#selector(showDresses:) forControlEvents:UIControlEventTouchUpInside];
bt.font=[UIFont systemFontOfSize:(CGFloat ) 13];
// bt.titleLabel.frame = CGRectMake(0, 0, 95,24);
bt.frame = CGRectMake(0, 3, 95,24);
[headerView addSubview:bt];
cell.accessoryView = headerView;

Resources