programmatically adding custom button messing with eachother - ios

I have a void I'm calling that shows hidden view on a cell when i swipe over, when they were al set to icons the positioning worked great, however changing them to custom buttons so i can have text instead of icons, had messed things up.
EDIT: also, if i touch where the buttons SHOULD be, it works as if there really there, it just "Looks" messed up. For example, if in the example where its wrong (image) how the decline1 is on the left, if i click it, it accepts the request, but if i click in the middle empty area, nothing.
This is the coding
- (void)bottomDrawerWillAppear {
UIImageView *drawerBGImg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,75)];
NSString *friendAvatar = [NSString stringWithFormat:#"%#%#%#", #"http://www.thatonewebsite.com/images/users/", [MyClass friendID], #".jpg"];
[drawerBGImg setImageWithURL:[NSURL URLWithString:friendAvatar]];
self.bottomDrawer.clipsToBounds = YES;
drawerBGImg.contentMode = UIViewContentModeScaleAspectFill;
[self.bottomDrawer addSubview:drawerBGImg];
UIImageView *drawerBG = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,75)];
drawerBG.image = [UIImage imageNamed:#"drawerBG.png"];
[self.bottomDrawer addSubview:drawerBG];
NSLog(#"%#", [MyClass friendID]);
UIButton *inviteToLocationBtn=[UIButton buttonWithType:UIButtonTypeCustom];
inviteToLocationBtn.frame=CGRectMake(15.0, 15.0, 80.0, 50.0);
[inviteToLocationBtn setTitle:#"accept" forState:UIControlStateNormal];
inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size: 22.0f];
inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
[inviteToLocationBtn addTarget:self action:#selector(callAccept) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:inviteToLocationBtn];
UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
[inviteToLocationBtn setTitle:#"decline1" forState:UIControlStateNormal];
inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size: 22.0f];
inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
[deleteBtn addTarget:self action:#selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:deleteBtn];
UIButton *messageBtn=[UIButton buttonWithType:UIBarButtonSystemItemCompose];
messageBtn.frame=CGRectMake(245.0, 15.0, 50.0, 50.0);
UIImage *messageImage = [UIImage imageNamed:#"messageIcon.png"];
[messageBtn setImage:messageImage forState:UIControlStateNormal];
[messageBtn addTarget:self action:#selector(callChat) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:messageBtn];
}
before you take a look, i apologize for the large images
this is how it looks
This is how its supposed to look, The middle image, invitation.

After you declare deleteBtn you set the title as #"decline1" but call inviteToLocationBtn. [inviteToLocationBtn setTitle:#"decline1" forState:UIControlStateNormal] should refer to deleteBtn.
this line above:
UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
[inviteToLocationBtn setTitle:#"decline1" forState:UIControlStateNormal];
inviteToLocationBtn.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size: 22.0f];
inviteToLocationBtn.titleLabel.textColor = [UIColor whiteColor];
[deleteBtn addTarget:self action:#selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:deleteBtn];
should be:
UIButton *deleteBtn=[UIButton buttonWithType:UIButtonTypeCustom];
deleteBtn.frame=CGRectMake(130.0, 15.0, 80.0, 50.0);
[deleteBtn setTitle:#"decline1" forState:UIControlStateNormal];
deleteBtn.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size: 22.0f];
deleteBtn.titleLabel.textColor = [UIColor whiteColor];
[deleteBtn addTarget:self action:#selector(callDeny) forControlEvents:UIControlEventTouchUpInside];
[self.bottomDrawer addSubview:deleteBtn];

Related

UIButton text of label not visible

Problem: If the user goes on the iPhone/iPad to "Settings - General - Accessibility - Increase Contrast" and switches on "Darken Colors" the text from my buttons disappears.
Here is my code for creating the buttons:
buttonCurrent = [UIButton buttonWithType:UIButtonTypeSystem];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor whiteColor];
[buttonCurrent setTitle:NSLocalizedString(#"Add the reading with the current date", nil) forState:UIControlStateNormal];
[buttonCurrent addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Thanks for the help.
I changed the code to:
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
[buttonCurrent setTitleColor:self.view.tintColor forState:UIControlStateNormal];
[buttonCurrent setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[buttonCurrent setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor grayColor];
[buttonCurrent setTitle:NSLocalizedString(#"Add the reading with the current date", nil) forState:UIControlStateNormal];
It looks now like the UIButtonTypeSystem and it works also with system setting "Darken Colors".
all code is fine except first line.
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTypeSystem have default blue color as a back ground in greater than iOS7. You should always use UIButtonTypeCustom, so that it does not shows the effect as you are experience.
And also please give frame. I checked your code and you have not given frame for button. Please do that first.
UIButton *buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
buttonCurrent.translatesAutoresizingMaskIntoConstraints = NO;
buttonCurrent.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
buttonCurrent.titleLabel.textAlignment = NSTextAlignmentCenter;
buttonCurrent.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonCurrent.titleLabel.numberOfLines = 2;
buttonCurrent.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
buttonCurrent.backgroundColor = [UIColor whiteColor];
buttonCurrent.frame = CGRectMake(0, 50, 320, 80);
[buttonCurrent setTitleColor:[UIColor colorWithRed:0.0 green:0.5 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
[buttonCurrent setTitle:#"Add the reading with the current date" forState:UIControlStateNormal];
[buttonCurrent addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonCurrent];
All code is fine except first line
buttonCurrent = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTypeSystem have default blue color as a back ground in greater than iOS7. You should always use UIButtonTypeCustom, so that it does not shows the effect as you are experience.

Title of a Custom UIBarButtonItem from a UIButton not showing

I made a custom UIBarButtonItem from a UIButton. The custom bar button is visible, the action works, the size is correct, and the background color can be seen; however the manually set title cannot be seen. maybe background is laid over the text? I'm not sure, help?
In viewDidLoad:
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
resetButton.titleLabel.text = #"RESET";
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
You should set the title for button of using setTitle:forState: for particular control states, replace your code with below
UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:#selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:#"ProximaNova-Bold" size:19];
[resetButton setTitle:#"RESET" forState:UIControlStateNormal]; //change this line in your code
resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;
UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];
self.navigationItem.rightBarButtonItem = resetBarBTN;
Use setTitle:forState to set button title. For example
[resetButton setTitle:#"text" forState:UIControlStateNormal]

UIButton pressed, other one doesn't press

hi I have some code that creates two buttons and when a button is pressed an trigger is set and it changes the background and the text colour.
How could i make it so that once one is pressed the other cannot be pressed.
This creates the buttons
//learnmore button
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:learnmorebutton];
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:#"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
//signup button
UIButton *signup = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.view addSubview:signup];
signup.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
signup.frame = CGRectMake(162.0, 518.0, 160.0, 50.0);
[signup setTitle:#"sign up" forState:UIControlStateNormal];
signup.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
signup.titleLabel.textColor = [UIColor whiteColor];
These are the two triggers
[learnmorebutton addTarget:self action:#selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
[signup addTarget:self action:#selector(signupClickEvent:) forControlEvents:UIControlEventTouchUpInside];
These are the two events
- (void)learnMoreClickEvent:(UIButton *)sender
{
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
- (void)signupClickEvent:(UIButton *)sender
{
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
thanks all ...
You start by keep a reference to the button, preferably properties on the viewController that creates these buttons.
#interface MyViewController : UIViewController
#property (nonatomic, strong) UIButton *learnmorebutton;
#property (nonatomic, strong) UIButton *signup;
#end;
Then in the method either disable the button:
<button>.enabled = NO:
Or hide it:
<button>.hidden = YES:
like rckoenes mentioned you can declare those UIButton as properties and do that.. or
Assign Tags like this
//learnmore button
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
learnmorebutton.tag = 10;
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:#"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
[learnmorebutton addTarget:self action:#selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:learnmorebutton];
//signup button
UIButton *signup = [UIButton buttonWithType:UIButtonTypeRoundedRect];
signup.tag = 20;
signup.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
signup.frame = CGRectMake(162.0, 518.0, 160.0, 50.0);
[signup setTitle:#"sign up" forState:UIControlStateNormal];
signup.titleLabel.font = [UIFont fontWithName:#"Helvetica Light" size:17.0];
signup.titleLabel.textColor = [UIColor whiteColor];
[self.view addSubview:signup];
[signup addTarget:self action:#selector(signupClickEvent:) forControlEvents:UIControlEventTouchUpInside];
By Using tags to access the buttons
- (void)learnMoreClickEvent:(UIButton *)sender
{
UIButton *signUp = (UIButton*)[self.view viewWithTag:20]
signUp.enabled = NO;
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
- (void)signupClickEvent:(UIButton *)sender
{
UIButton *learnmorebutton = (UIButton*)[self.view viewWithTag:10]
learnmorebutton.enabled = NO;
//sender is the button that was tapped
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
sender.backgroundColor = [UIColor whiteColor];
}
Hope this gives you an Idea.
Basically if your looking for toggling, you can always use UISegementControl. I mean it depends on your requirements.
Set the enabled property of the button you want to disable to NO: <button>.enabled = NO;

UIButton shows tintColor only when being pressed, not in normal state

Here's my code. It creates a white button that turns purple on being pressed. What I want is a purple button.
if (!backButton) {
backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue" size:17];
CGSize size = [aboutButton.titleLabel.text sizeWithFont:[UIFont fontWithName:#"Helvetica Neue" size:23]];
backButton.frame = CGRectMake(screenWidth/2 - size.width/2, screenHeight-size.height*4, size.width, size.height);
backButton.tintColor = [UIColor colorWithRed:123/255.0 green:47/255.0 blue:85/255.0 alpha:1]; //This is the color I want the button to be in its normal state.
[backButton addTarget:self action:NSSelectorFromString(#"displaySettingsScreen") forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:backButton];
What am I missing?
Sadly, the tintColor property doesn't work for RoundedRect button types.
See here:
Why is my UIButton.tintColor not working?
To get around this, I use a single-segment UISementedControl, in place of the UIButton I wanted colored.
This answer here describes the technique:
https://stackoverflow.com/a/4971371/785411
Another alternative would be to use a purple image for the button - though that would require a bit more work.
Rounded rect buttons are hard to change, so use a custom button instead, and set the background color:
if (!backButton) {
backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont fontWithName:#"Helvetica Neue" size:17];
CGSize size = [aboutButton.titleLabel.text sizeWithFont:[UIFont fontWithName:#"Helvetica Neue" size:23]];
backButton.frame = CGRectMake(screenWidth/2 - size.width/2, screenHeight-size.height*4, size.width, size.height);
backButton.layer.cornerRadius = 6.0f;
backButton.backgroundColor = [UIColor colorWithRed:123/255.0 green:47/255.0 blue:85/255.0 alpha:1]; //This is the color I want the button to be in its normal state.
[backButton addTarget:self action:NSSelectorFromString(#"displaySettingsScreen") forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:backButton];
You'll need to add the QuartzCore framework, and import it (for the layer properties to work).

iPad develop:customized button on navigationBar can't see after push->pop

iPad app:I've add some customized button to navigationBar as follows:
- (void)addEditButton{
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, 50, 30);
[editButton setBackgroundImage:[[UIImage imageNamed:#"images_iPad.bundle/NavigationBar_Btn_iPad.png"] stretchableImageWithLeftCapWidth:15/2.0 topCapHeight:32/2.0] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
[editButton setTitle:#"Edit" forState:UIControlStateNormal];
editButton.titleLabel.font = [UIFont fontWithName:kAPPBOLDFONT size:12];
editButton.titleLabel.shadowColor = [UIColor blackColor];
editButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
UIBarButtonItem *editItem = [[[UIBarButtonItem alloc] initWithCustomView:editButton] autorelease];
self.navigationItem.rightBarButtonItem = editItem;
}
My problem is that:I clicked this button,it will push to another viewController, after popModelViewController:animate,I can't see this customized button, although this, it does work.and not just here ,others are the same,please help me.

Resources