I am developing a Universal app, and there is a UIButton in it which works fine with iPhone but when I click it in iPad it requires many click attempts to get the touch event occur.
e.g. after 5-6 clicks it executes click event.
Below is UIButton code. Please help.
UIView *footer = [[[UIView alloc] initWithFrame:(CGRectMake(0, 0, self.tableView.frame.size.width, 54))] autorelease];
float buttonWidth = (int)((self.tableView.frame.size.width - 12 - 12) / 3);
float buttonHeight = 44;
if (clientState.Devicetype == 1) // 1=Ipad
buttonHeight = 90;
cash = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
cash.frame = CGRectMake(6, 10, buttonWidth, buttonHeight);
[cash setTitle:#"Cash" forState:UIControlStateNormal];
[cash setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
[cash addTarget:self action:#selector(handleCash:) forControlEvents:UIControlEventTouchUpInside];
cash.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
if (clientState.Devicetype == 1) // 1=Ipad
{
cash.titleLabel.font = [UIFont systemFontOfSize:28];
}
[footer addSubview:cash];
self.tableView.tableFooterView = footer;
You should check to make sure that your buttons are the correct size:
[cash sizeToFit]
I've experienced that if some other view is blocking it (and you click on that part of your button it won't fire off the event) it will take several clicks because you eventually press the button where it's not being covered. So make sure no other frame/bounds is over the top of your button and make sure your button is sized correctly.
I agree with #Inturbidus that it's probably another transparent view covering part of the button. I find the best way to troubleshoot this is to change the background color on all my other views (scrollviews, etc) to make sure they are behaving like I think they are. Sometimes it will surprise you when you see where they are actually positioned with background color turned on.
Related
Do you know, how to control the ScrollView, so they do not repeat. For example I have 5 pictures (like the added figure), they should be viewable from 1-5 and it should stop right after the last picture, at the same time it should be able to scroll back and stop right after the first picture. All in all it should only be able to go from 1-5 without repeating after 5th picture and before 1st.
The problem right now is, that it scroll further after 5th, so it begins from 1st picture again, and the other way around when it scroll back to 1st picture it keep going to 5th and so on.
The code that I used for this is:
"InfinitePagingView" page!.
Disabled scroll:
scrollEnabled = NO;
What I want to know is, if I am able to stop scrolling right after 5th picture, but it should only be enable to do that on the right side. So is there any codes like: "scrollEnable right = NO & scrollEnable left = NO". If not, which code can I use.
Advance thanks
so you want infinite scrolling on the right side, but not on the left? You need to customize the InfinitePagingView, that you are using. Look in the code, especially the method scrollView:didScroll and disable it for the left side.
That is not a default behaviour, so there is not a simple property to set. But don't you think this is a strange behaviour? If you scroll right and go over the 5th picture, than you are at the 1st one again and you cannot scroll to the left anymore? Although you just came from that place?
If you don't want any infinite scrolling just use a default scrollView!
I have using following Code to display 5 UIButtons in Scrollview it works for as you want.you can use it for 5 pictures Check it.
- (void)viewDidLoad
{
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 407, 320, 80)];
[self.view addSubview:scrollview];
int i;
int spacex = 5;
int spacey=5;
for ( i = 0; i <5; i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn = [[UIButton alloc] initWithFrame:CGRectMake(spacex, spacey, 115, 45)];
btn.backgroundColor = [UIColor greenColor];
[btn setTitleColor: [UIColor blackColor] forState: UIControlStateNormal];
spacex = spacex + 120;
[scrollview addSubview:btn];
}
scrollview.contentSize = CGSizeMake(scrollview.frame.size.height*(i+2) , scrollview.frame.size.height);
[super viewDidLoad];
}
You can changed it according to your requirments.hope it works for you.Thanks.
I want to create a small horizontal scrollview with few buttons as shown in image. User can select any one of them and details will be shown accordingly. I am not sure how to code it programmatically. Please help.
Image:- http://i.stack.imgur.com/uDqkn.jpg
ok follow steps to implement the same.
step 1 : take UITableview and apply affin transform for 90 degree.
step 2 : take a button on each cell also apply affinetransform of 90 degree.
step 3 : for each table cell height (now you can say width) will be calculated with the help of stringwidth function of NSString. so please type code in call back method (heightforRowatIndexPath).
step 4 : take layer of tableview and apply round corner property with value 10.0f
step 5 : same you can do for the button their color.
step 6: for outer arrow yo need to implement some UIScrollview module.
here all memory will be managed by tableview for n number of buttons in horizontal scrolling.
Thanks,
Please let me know for any issue
In an ARC view controller ...
#define kHEIGHT 35.0
- (void)createButtonScrollViewWithButtonTitles:(NSArray *)titles {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0,100.0,320.0,kHEIGHT)];
CGFloat buttonPositionX = 0.0;
for (NSString *title in titles) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(pressedButton:) forControlEvents:UIControlEventTouchDown];
[button setTitle:title forState:UIControlStateNormal];
button.frame = CGRectMake(buttonPositionX, 0.0, 90.0, kHEIGHT);
buttonPositionX += 90.0;
[scrollView addSubview:button];
}
scrollView.contentSize = CGSizeMake(buttonPositionX, kHEIGHT);
[self.view addSubview:scrollView];
}
After you get this working, you can create a version that takes an array of images. For that, the button type will change to UIButtonTypeCustom. You'll use setImage: rather than setTitle: and you'll have the chance to vary the width by advancing buttonPositionX by the image.size.width, rather than a constant.
Not sure how to interpret the little scroll-arrow-looking things on each side of the image you posted. Another subsequent enhancement would be to overlay each end with a little image view (with userInteractionEnabled = NO) that matches those arrow things.
A button can be added by:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(100, 100, 160, 50);
[myButton setTitle:#"click me" forState:UIControlStateNormal];
[self.view addSubview:myButton];
but can we go with the initWithFrame method:
UIButton *myButton2 = [[UIButton alloc] initWithFrame:CGRectMake(100, 300, 160, 50)];
//myButton2.buttonType = UIButtonTypeRoundedRect;
myButton2.titleLabel.text = #"click me";
[self.view addSubview:myButton2];
but line 2 above is commented out because it causes an error for a "read only" property, and with that line commented out, still no button shows up at all... can a UIButton be added if using initWithFrame to begin with?
(update: I set a yellow background to see the view's area, and found that the label text of button 2 actually shows up as "click me" in white... but touching it has no visual effect... so I wonder how to change code sample 2 to make it work...)
Yes of course it can. I got into a little bit of a discussion as to that readonly buttonType property a while back. The general concensus is that there is neither a safe way, nor a good reason, for anyone to switch the type of a button. And besides, +buttonWithType returns an instance of UIButton just as well as +Alloc and -init do, so it isn't a problem.
Can we add activity indicator as subview to UIButton?
If yes then plz tell me how to do that?
I used [button addSubview:activityIndicator];
Not working...
I found that adding a UIActivityIndicatorView to a UIButton was a really useful method to allow users to know something is happening without having to use the MBProgressHUD (I think the HUD is really good but should not be used in all situations.
For this reason I created two functions:
I have already allocated my UIButton so it is a class variable called _confirmChangesButton
I then create my activity indicator, set its frame (taking into account the button size) and then adding the indicator is easy.
- (void)addActivityIndicatorToConfirmButton {
// Indicator needs to be in the middle of the button. So half the screen less half the buttons left inset less half the activity indicator size
CGRect rect = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 10 - 15, 5, 30, 30);
UIActivityIndicatorView * activity = [[UIActivityIndicatorView alloc] initWithFrame:rect];
activity.hidesWhenStopped = YES;
[_confirmChangesButton setTitle:#"" forState:UIControlStateNormal];
[_confirmChangesButton addSubview:activity];
[activity startAnimating];
}
Having a removal function is also useful if you are using blocks. It might be that the completion task comes back with a failure and so we want to remove the indicator and change the title back. In this function we need to make sure to remove the indicator and not the button label which is the other subview on this button.
- (void)removeActivityIndicatorFromConfirmButton {
UIActivityIndicatorView * activity = _confirmChangesButton.subviews.lastObject;;
[activity removeFromSuperview];
[_confirmChangesButton setTitle:#"Confirm Change" forState:UIControlStateNormal];
}
I found that using these two you can create a much better user experience letting the user know what is going on when they press buttons.
Hope this helps
Use the below code below to add acitivity indicator a button or any uiview object
UIActivityIndicatorView *aView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
aView.frame = CGRectMake(0, 0, {yourButton}.frame.size.width, {yourButton}.frame.size.height);
aView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
[{yourButton} addSubview:aView];
[aView startAnimating];
Hope this will help..
I don't think it's possible to add a view to a button. UIButton have this method because it's inherited from UIVIew.
The real question is : why do you want to add an activity indicator on a button and not elsewhere ?
did you do [activityIndicator startAnimating]; ALso as u are using it in a tableview just check if the tags are set properly
I have a UIToolbar that I've customized with my own background image. Consequently, the built-in UIBarButtonItem appearance doesn't work for me, so I'm using images that are already prepared to show in the bar. I create a custom button item with this method:
+ (UIBarButtonItem *)customWithImage:(UIImage *)image enabled:(BOOL)enabled target:(id)target action:(SEL)selector {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//I've tried different values for the frame here, but no luck
button.frame = CGRectMake(0, 0, 44, 44);
button.enabled = enabled;
button.showsTouchWhenHighlighted = YES;
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
[button setImage:image forState:UIControlStateNormal];
UIBarButtonItem *it = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
//Tried changing this, to no avail
it.width = 32.f;
return it;
I have one button on the left and one on the right and I'm trying to make it so that if you tap on the far left or far right of the UIToolbar, the corresponding button is tapped. However, with these custom buttons, the hit targets do not extend all the way to the edges of the UIToolbar, but are inset from the sides:
http://skitch.com/andpoul/d1p8g/hit-targets
Any help greatly appreciated!
UIBarButtonItem.width might be ignored if you're using a custom view (it probably just uses the width of the view).
A lame hack is to make the toolbar wider (so it sticks outside the screen) and add transparent edges to the background image to compensate. This brings the buttons closer to the edge of the screen.
An alternative is just to use a UIImageView with UIButton subviews.
I think the only way you will have to go is to make buttons wider (change image by adding some from left for one and right for another) and adjust size...