Programmatically created UIButton not visible - ios

I have created a UIButton, added it to a view, which has then been added to a UIScrollView. This View also contains a UITextView which displays and is formatted correctly.
My code is below. Hopefully someone can help. The area where the button should be is clickable, and acts correctly, however the button is not visible. I even changed the tintColor to red, but I cannot see it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *billBlocker = *UIView initialized*
//The UITextView is a subview of `billBlocker`, as well as the `billBlockButton`
//added the UITextView here
UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30 )];
[billBlockButton setTitle:#"BillBlockerFrenzy" forState:UIControlStateNormal];
[billBlockButton setTintColor:[UIColor redColor]];
[billBlockButton addTarget:self action:#selector(segueToRegulationsGame) forControlEvents:UIControlEventTouchUpInside];
[billBlocker addSubview:billBlockButton];
[self.scrollView addSubview:billBlocker];
}

You should use cluster method when crate button:
UIButton *billBlockButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[billBlockButton setFrame:CGRectMake(0, 5,292,30 )];
...
This code should work. Make sure the scrollView frame and content size is set correctly and scrollView is added to self.view.
buttonWithType: is recommended method for creating the button, this is the only way you can specify button type.
When you use alloc init, I believe, you use some standard button type and if iOS change the standard also could change.

Probably the problem is you forgot the set outlet of scroll view i tried and its working like this
#property (strong,nonatomic) IBOutlet UIScrollView *sView; //scrollviews outlet
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//added the UITextView here
UIButton *billBlockButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 5,292,30 )];
[billBlockButton setTitle:#"BillBlockerFrenzy" forState:UIControlStateNormal];
[billBlockButton setBackgroundColor:[UIColor redColor]];
[billBlockButton addTarget:self action:#selector(nothing) forControlEvents:UIControlEventTouchUpInside];
[self.sView addSubview:billBlockButton];
}
Forgot to say you need to add your method instead of nothing method..
Hope it helps.

When we create the UIButton object programmatically, the default title color of UIButton is white.
So, set the title color of UIButton object.
[billBlockButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

iOS 7 or later will have transparent button. Unless you set text, you will not be able to see. Do following:
[[yourButton layer] setBorderColor:[UIColor blackColor].CGColor];
[[yourButton layer] setBorderWidth:2.0];
[yourButton setTitle:#"Hello" forState:UIControlStateNormal];

Related

UIButton.titleLabel.frame.size always with zero values

i develop one project in xcode on Objective C. All was fine before last two days. Now all my buttons and other controls do not show their title text or images. I try to add new button from storyboard but it text still not showing.
Also i try to add new button from code but result the same.
- (void) addTestButtonProgramatically {
testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton addTarget:self
action:#selector(testButtonSelector:)
forControlEvents:UIControlEventTouchUpInside];
[testButton setTitle:#"TEST TEXT" forState:UIControlStateNormal];
[testButton setBackgroundColor:[UIColor colorWithRed:0.6 green:0.1 blue:1.0 alpha:1.0]];
testButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:testButton];
}
- (IBAction)testButtonSelector:(id)sender {
[testButton.titleLabel sizeToFit]; //this row make text of my button shown
}
Maybe i mistakenly changed the setting which made such behavior. I try to lunch my project on another computer and issue is still there too.
Then i try to create new clear project and there everything was fine, text of buttons is visible.
Edit:
I find cause of my issue. It was because i redefine - (void) layoutSubvies method on UIButton category.
#interface UIButton (Coordinator)
#end
#implementation UIButton (Coordinator)
- (void) layoutSubviews {
[super layoutSubviews];
}
#end
Thens for all who help me find that.
add [testButton.titleLabel sizeToFit] to addTestButtonProgramatically method. why are you fitting size of button's text in it's own click..?!. you should do it when you setup the button
Update :
[self.button layoutIfNeeded]; // call this after adding button in view if you are using autolayout

Change properties of programmatically created UIButton in a different IBAction

I have one method that creates a number of UIButton instances programmatically and positions them in the view. Another IBAction needs to change the background colour of one of these UIButton when fired however when I try to action it, it tells me that the Property not found on object of type UIView *
The button is created using:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[view addSubview:button];
When I try to access it from the other IBAction like:
button.backgroundColor = [UIColor blackColor];
It fails with the error noted above.
Any help is much appreciated. I saw another answer which suggested to create an array as an IBOutlet and access it that way but I'm wondering if there's another way.
Thanks!
declare this button in globally and identify each button in tag, just like or assume this
UIButton * button, *button2; // declare in your view controller.h
- (IBAction)butaction:(id)sender; //this is your color change method;
in your view controller.m wherever u add this button
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=10; //this is important
[self.view addSubview:button];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.tag=20; //this is important
[self.view addSubview:button1];
//here ur color change method
- (IBAction) butaction:(UIButton*)sender {
switch (sender.tag)
{
case 10:
[button setBackgroundColor:[UIColor blackColor]];
break;
case 20:
[button1 setBackgroundColor:[UIColor blackColor]];
break;
default:
break;
}
}
Try this, it may be helpful
for (int index=1; index<=5; index++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(ButtonClickedFunction:) forControlEvents:UIControlEventTouchUpInside];
button.tag= index;
[view addSubview:button];
};
implement this in IBAction
-(IBAction)ButtonClickedFunction:(UIButton *)button{
button.backgroundColor = [UIColor blackColor];
}
Note: In this I have created 5 buttons but not set frame. Please set frame according to your requirements.
Thanks

UIButton has dot when set to selected inside UIToolbar

I'm adding a UIButton to a UIToolbar that I don't want to have as a UIBarButtonItem so that I have more control over the position of the UIButton inside the frame. I set a different title for both "Normal" and "Selected" states. Everything acts completely normal for a UIButton except when it's "Selected" there is a 1 pixel dot in the front of the UIButton's title.
toolbar:
UIToolbar *customView = [[UIToolbar alloc] initWithFrame:CGRectMake(HEADER_MARGIN, 0, HEADER_WIDTH, HEADER_HEIGHT)];
[customView setBarTintColor:[UIColor whiteColor]];
button:
UIButton *listOrderButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ORDER_BUTTON_WIDTH, ORDER_BUTTON_HEIGHT)];
[listOrderButton setCenter:CGPointMake(customView.center.x, HEADER_HEIGHT/2)];
[listOrderButton.titleLabel setFont:[UIFont systemFontOfSize:ORDER_BUTTON_FONT_SIZE]];
[listOrderButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
[listOrderButton setTitle:#"ABC" forState:UIControlStateNormal];
[listOrderButton setTitle:#"Time" forState:UIControlStateSelected];
[listOrderButton setTitleColor:[UIColor blueAppColor] forState:UIControlStateNormal];
[listOrderButton addTarget:self action:#selector(listOrderChange:) forControlEvents:UIControlEventTouchUpInside];
[customToolbar addSubview:listOrderButton];
and the selector:
- (void)listOrderChange:(UIButton *)sender {
[sender setSelected:!sender.selected];
}
Image of what's happening:
- No dot when not selected
- When selected, there is a dot
This happens both on the device and in the simulator. Changing the text color for any and all states has no bearing on the color of the dot itself.
I should also point out that the dot is two points wide and one point high.
Instead of using UIButton, I know you dont want to use a UIBarButton, but would it be possible to create a UIBarButton programmatically giving it all the features of a UIButton?
Since UITabBar is like UINavigation Bar and apple suggests using UIBarButtonItem for bar/tabBar Buttons... I believe using a UIBarButtonItem might solve the problem....

Cannot hide programmatically created UIButton

This issue is baffling me for a few days now.
I have 2 UIButtons, one created using the Storyboard and one created programmatically.
When I press one of the buttons, I am moved to a method that hides both the programatically built and storyboard buttons, but only the storyboard button disappears, whereas the programmatically built button remains. I have tried not only hiding but also changing the frame of the programmatically built button, but to no avail.
Here is the code used:
#property (strong, nonatomic) UIButton *catBut;
#property (weak, nonatomic) IBOutlet UIButton *whenButton;
....
....
-(void)viewDidLoad {
....
[self customizeButton:self.catBut withFrame:CGRectMake(165, 113, 135, 50)
andAction:#selector(selectedCat) andColor:[UIColor belizeHoleColor]
andTag:2 andImage:#"coffee-64.png" andText:#"Cafes" andAlignmentLeft:YES];
....
}
- (void)customizeButton:(UIButton *)but withFrame:(CGRect)rect andAction:(SEL)action
andColor:(UIColor *)col andTag:(NSUInteger)tag
andImage:(NSString *)imageName
andText:(NSString *)buttonText
andAlignmentLeft:(BOOL)alignLeft {
but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setFrame:rect];
[but setTitle:buttonText forState:UIControlStateNormal];
but.tag = tag;
but.titleLabel.numberOfLines = 0;
but.titleLabel.font = [UIFont boldFlatFontOfSize:18];
[but setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
CGFloat spacing = 10; // the amount of spacing to appear between image and title
but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
but.imageEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
but.titleEdgeInsets = UIEdgeInsetsMake(0, spacing*2, 0, 0);
[but setTitleColor:[UIColor cloudsColor] forState:UIControlStateNormal];
[but setTitleColor:[UIColor cloudsColor] forState:UIControlStateHighlighted];
[but addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[self.buttonsView addSubview:but];
}
....
- (void)selectedCat {
self.catBut.hidden = YES;
self.whenButton.hidden = YES;
}
Why is only the Storyboard button compliant to UI changes? Is there something I am missing? I have tried cancelling the AutoLayout but that didn't change a thing.
The issue is with the passing for argument in your method (customizeButton:(UIButton *)but).
Change your code to this and check
but = [UIButton buttonWithType:UIButtonTypeCustom];
to
self.catBut = [UIButton buttonWithType:UIButtonTypeCustom];
......
[self.buttonsView addSubview:self.catBut];
OR You should pass the address customizeButton:(UIButton **)but in the method
and while calling use &self.catBut
It is BOOL type, not an bool type.
try this
self.catBut.hidden = YES;
self.whenButton.hidden = YES;
I can see this property catBut, But I cann't see creation for this, as well I didn't give Outlet too. Check this. You've tried something but simply fix by following line.
[self.buttonsView addSubview:but];
self.catBut = but;

How do I access a button in a subview

I want to implement this fade in/out in a horizontal scroll I have, but I can't figure out how to access my button that is in a subview of my UIScrollView. This is what I'm trying to implement Fade in/out stackoverflow.
This is my code...
Game *game = (Game *)[_schedule.games objectAtIndex:i];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, 150, 100)];
[button setBackgroundImage:[UIImage imageNamed:game.opponent] forState:UIControlStateNormal];
//[button setTitle:game.opponent forState:UIControlStateNormal];
[_gameScrollList addSubview:button];
How Can I add the observer to my button, Can this be done?
[self.scrollView addObserver:[self.contentViews objectAtIndex:i]
forKeyPath:#"contentOffset"
options:NSKeyValueObservingOptionNew
context:#selector(updateAlpha:)];
To access the reference of this button added as subview, you can:
Use this button as a iVar, saving the reference to use it later;
Similarly, use this button as a property;
Iterate through subviews of _gameScrollList, looking for a subview of UIButton's class (not recommended, will work well only if it has just one UIButton as subview)
Example of how implement the second approach. Just declare a property:
#property(nonatomic, strong) UIButton *button;
and then:
Game *game = (Game *)[_schedule.games objectAtIndex:i];
self.button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, 150, 100)];
[self.button setBackgroundImage:[UIImage imageNamed:game.opponent] forState:UIControlStateNormal];
//[self.button setTitle:game.opponent forState:UIControlStateNormal];
[_gameScrollList addSubview:self.button];
So, after, when you want to access this button somewhere, just call for the property
self.button.alpha = 0.5; //example of interation with your button
If this is not what you are looking for, you really should edit your question to be more clear.
Hope it helped.

Resources