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
Related
Working off of other Stack Overflow answers, I have managed to come up with the code below:
(in my homescreen.m file)
UIButton *GCButton;
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
//add button
[self addGameCenterButton];
}
return self;
}
-(void)addGameCenterButton
{
GCButton = [[UIButton alloc]initWithFrame:CGRectMake(50, 20, 30, 30)];
[GCButton setBackgroundColor:[UIColor orangeColor]];
[GCButton setTitle: #"My Button" forState:UIControlStateNormal];
[GCButton setTitleColor: [UIColor blueColor] forState:UIControlStateNormal];
[GCButton.layer setBorderWidth:1.0f];
[GCButton.layer setBorderColor:[UIColor blueColor].CGColor];
//adding action programatically
[GCButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:GCButton];
}
-(void)buttonClicked:(UIButton*)sender
{
NSLog(#"you clicked on button %ld", (long)sender.tag);
}
As of right now, I see nothing on my screen. Is there anything else I need to add in my .m or .h files to make the button work?
From the comments of your question, I'm assuming you are using SpriteKit. In a GameScene, unfortunately, you cannot use UIButtons normally.
To create buttons for your scene, please check the answers in the link below:
Setting up buttons in SKScene
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];
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
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;
I have some custom type UIButtons placed pragmatically in a custom view. The view is basically a menu. I am generating the buttons with the following code, inside the view's .m file:
-(void) generateButtons: (NSMutableArray *) buttonsArray
{
UIImage *barItemImage = [[UIImage imageNamed:#"ipad-menubar-button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
float buttonWidth = (self.frame.size.width - (2 * FIRSTBUTTONXCOORDINATE) - 25)/ 6.0f;
float xCoord = FIRSTBUTTONXCOORDINATE;
float yCoord = (self.frame.size.height - BUTTONHEIGHT) / 2.0f;
for (int i = 0; i < buttonsArray.count; i++)
{
NSString* buttonTitle = [buttonsArray objectAtIndex:i];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(botButtonAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:buttonTitle forState:UIControlStateNormal];
button.frame = CGRectMake(xCoord, yCoord, buttonWidth, BUTTONHEIGHT);
button.tag = i;
[button setBackgroundImage:barItemImage forState:UIControlStateNormal];
xCoord += buttonWidth + 5;
[self addSubview:button];
}
}
On touchUpInside, botButtonAction is called and it does what it is supposed to. However, the buttons do not usually give any visual indication of the touchUpInside. When I touchDown on a button and swipe in any direction, then I see the background color get darker. Sometimes, I see the same visual indication on touchUpInside. It seems completely random though.
Any ideas as to what I might be doing wrong here?
Edit: Sorry, I got occupied with other issues. Here is the code for botButtonAction and the method it calls
-(void) botButtonAction: (UIButton *) sender
{
if (self.delegate)
[self.delegate optionsMenuAction:self data:sender.tag];
}
-(void)optionsMenuAction:(OptionsMenueView *)view data:(int)tag
{
if (self.PDFVC)
{
[self.navigationController popToViewController:self animated:YES];
}
if (!self.optionsMenue.isAtWPC || tag != WPC)
[self transitionFromViewAtIndex:self.selectedVCIndex toView:tag];
else
[self transitionFromViewAtIndex:self.selectedVCIndex toView:WPList];
}
But since I am using touchUpInside, these methods don't get called on touchDown. I just want the button to be highLight (grayout the bg a little) on touchDown. As I state before, that happens on touchDown and swipe.
Edit
I had so far been running this on actual devices (iPad air and iPad 2). When I tried running it on the simulator, I get the correct result. On mouse click, the button gets highlighted.
Edit
I think I figured out the exact problem (still looking for a solution). If I touchDown precisely on the button and the touch does not cover any part of the insets, then the highlighting works. However, if the touch covers part of the button and also part of the insets/outside, then the highlighting doesn't work.
add this line:
[button setBackgroundImage:HighlightImage forState:UIControlStateHighlighted];
change the title colour when tap on the button like this
[yourButon setTitleColor:[UIColor colorYouWant] forState:UIControlStateHighlighted];
hope this will help you......
put this code in botButtonAction:
-(void)botButtonAction:(UIButton *)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"some iamge.png"]
forState:UIControlStateNormal];
}
or use the code below code in for loop
[button setBackgroundImage:[UIImage imageNamed:#"back_button_press.png"] forState: UIControlStateHighlighted];
or if you want to give some color then setcolour of button
I had this problem at some point too. These other answers will give you the result you seek normally. However.... You are using [UIButton buttonWithType:UIButtonTypeCustom].. Which is really buggy if you do not alloc/init the UIButton.. Try changing this line to a normal button allocation and then add the following lines of the answers in this post. This should work for you.
So in short,
replace [UIButton buttonWithType:UIButtonTypeCustom];
with [[UIButton alloc] init];
And then add the following changes to your code to change the image.
[myNewlyAllocatedButton setBackgroundImage:[UIImage imageNamed:#"back_button_press.png"] forState: UIControlStateHighlighted];