ios : subview of a button not showing up - ios

My code is as follows:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect frame = CGRectMake(50, 125, 220, 40);
button.frame = frame;
button.layer.cornerRadius = 20;
button.backgroundColor = [UIColor colorWithRed:0.42 green:0.66 blue:0.31 alpha:1.0];
button.layer.masksToBounds = YES;
[self.view addSubview:button];
button.autoresizesSubviews = NO;
button.clipsToBounds = YES;
UILabel *addLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, 100)];
addLabel.text = #"+";
addLabel.textColor = [UIColor blackColor];
addLabel.textAlignment = NSTextAlignmentCenter;
addLabel.font = [UIFont fontWithName:#"HelveticaNeue-UltraLight" size:80.0f];
[button addSubview:addLabel];
addLabel.autoresizesSubviews = NO;
addLabel.clipsToBounds = YES;
My button is showing up fine in iOS/iPhone simulator, however addLabel label is not showing up at all.
What am I missing?

The frame on your label is too large. The label text is being drawn outside the frame of your button as a result. Ideally, the frame of your label would be at most as tall as the button height.

Well UIButton already has a titleLabel. Have you tried just using that instead?
button.titleLabel.text = "+";
or even using a different button type:
UIButton* button = [UIButton buttonWithType:UIButtonTypeContactAdd];

Related

iOS: UIButton draw circle border in iOS

I need to draw a circle like border outside UIButton. Below is the attach image and code. I also need to show text below button. Following code will add image and text beneath that. But how do I have layer.
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setBackgroundImage:image forState:UIControlStateNormal];
imageButton.translatesAutoresizingMaskIntoConstraints = NO;
imageButton.backgroundColor = [UIColor clearColor];
[imageButton setTitle:title forState:UIControlStateNormal];
imageButton.titleLabel.textAlignment = NSTextAlignmentCenter;
imageButton.titleLabel.font = [UIFont fontWithName:#"OpenSans" size:fon];
[imageButton setTitleEdgeInsets:UIEdgeInsetsMake(95, 0.0f, 0.0f, 0.0f)];
[imageButton setTitleColor:[UIColor iconTextColor] forState:UIControlStateNormal];
[imageButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
set the button width and height ex : width =100 and height = 100, should be same if you want a round then,
imageButton.layer.cornerRadius = 50 // this value should be half from width or height (width = height)
imageButton.layer.masksToBounds = YES
//if you want border with above
imageButton.layer.borderWidth = 1;
imageButton.layer.borderColor = [[UIColor blackColor] CGColor];
You can do something like this.
borderView = UIView()
yourButton = UIButton()
borderView.frame.size.width = yourButton.frame.size.width + 1
borderView.layer.cornerRadius = borderView.frame.size.width/2
borderView.clipToBounds = true
borderView.backgroundColor = UIColor.clearColor()
borderView.layer.borderWidth = 1
borderView.layer.borderColor = UIColor.blueColor()

Button Text not showing

When I try to post the following code, my app just shows an empty rectangle where the button's text should be. I am a beginner at xcode and I have determined this code from other tutorials.
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *player1 = [[UILabel alloc]initWithFrame:CGRectMake(35, 120,130, 30)];
[player1 setText:#"Player"];
player1.layer.borderWidth = 1.0;
player1.layer.borderColor = [UIColor blackColor].CGColor;
player1.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player1];
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
[score1 setTitle:#"0" forState:UIControlStateNormal];
score1.layer.borderWidth = 1.0;
score1.layer.borderColor = [UIColor blackColor].CGColor;
[self.view addSubview: score1];
UILabel *player2 = [[UILabel alloc]initWithFrame:CGRectMake(35, 150, 130, 30)];
[player2 setText:#"Opponent"];
player2.layer.borderWidth = 1.0;
player2.layer.borderColor = [UIColor blackColor].CGColor;
player2.textAlignment = UITextAlignmentCenter;
[self.view addSubview: player2];
}
Like I said, everything but the text in the button is showing up.
The reason why the button looks blank is because its text color is actually white. Just give it a different color and 0 will now show:
score1.layer.backgroundColor = [UIColor blackColor].CGColor;
If you don't want to change the button background color, you may want to set the text color:
[score1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Read Question: [initWithFrame:frame] vs. [UIButton buttonWithType], for more about creating a UIButton with these different methods.
The problem is the way you're creating the button:
UIButton *score1 = [[UIButton alloc]initWithFrame:CGRectMake(165, 120, 60, 30)];
That's not how you do it. Do it like this:
UIButton *score1 = [UIButton buttonWithType:UIButtonTypeSystem];
score1.frame = CGRectMake(165, 120, 60, 30);
All will be well after that!

UIButton's title property logs correctly but not showing up when adding button as subview

I have a UIButton that I have created, and I can successfully add it as a subview and see it on the screen. The problem is, when I try using:
[myButton setTitle:#"My Title" forState:UIControlStateNormal];
The button does not show a title when I run the app. If I NSLog the button's title after using the above method, it has in fact been set to the title that I'm passing in.
Here's my code. This is inside a UIView subclass. I realize that some of this logic might not belong in a UIView subclass but I'm just trying to get this to work as fast as possible:
// Set the view's frame, background color, corner radius
self.frame = CGRectMake(45, 200, 235, 187);
self.backgroundColor = [UIColor whiteColor];
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor grayColor].CGColor;
self.layer.cornerRadius = 5;
// Create the popup's body text label
self.popupBodyTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 150)];
self.popupBodyTextLabel.text = #"Text goes here.";
// Add text view to popup's subviews
[self addSubview:self.popupBodyTextLabel];
// Create ok button
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 143, 120, 44)];
myButton.backgroundColor = [UIColor whiteColor];
myButton.layer.borderWidth = 1;
myButton.layer.borderColor = [UIColor grayColor].CGColor;
[myButton setTitle:#"OK" forState:UIControlStateNormal];
// Round the button's bottom left corner
UIBezierPath *myButtonMaskPath = [UIBezierPath bezierPathWithRoundedRect:myButton.bounds byRoundingCorners:(UIRectCornerBottomLeft) cornerRadii:CGSizeMake(5.0, 5.0)];
CAShapeLayer *okButtonMaskLayer = [[CAShapeLayer alloc] init];
myButtonMaskLayer.frame = myButton.bounds;
myButtonMaskLayer.path = myButtonMaskPath.CGPath;
myButton.layer.mask = myButtonMaskLayer;
// Add selector to button
[myButton addTarget:self action:#selector(myButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
// Add button to subviews
[self addSubview:myButton];
// Create the background view
self.backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
self.backgroundView.backgroundColor = [UIColor grayColor];
self.backgroundView.alpha = 0.5f;
// Show our new views
[[[UIApplication sharedApplication] keyWindow] addSubview:self.backgroundView];
[[[UIApplication sharedApplication] keyWindow] addSubview:[MPPopupView shared]];
The code for setting button title is right, so use this code to set title color
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

Add UIImage or UILabel to navigationItems

In my app, I have to customize my navigation bar depending on the page I'm on. In some views, I need to add some UIButton, but also some UIImage (for the logo that is not centered) or some UILabel.
I could do that using [self.navigationController.navigationBar addSubview:myLabel]. But I don't really like this method because if I ever have to add a UIButton or just change the text of the UILabel, I need to change manually the origin of each UILabel and each UIImage.
I thought of using something that already exists : UIBarButtonItem. It can contain some text, an image, and I just need to add each of them in the navigationItem.leftBarButtonItems, and every thing is nicely positioned.
The only problem that remains, is that it still is a button, and reacts as a one when pressed. If I disable it, it's alpha changes to show it's disabled... How could I keep it's normal apparence, and let it disabled ?
Thanks !
PS: I'm open to any other idea to add UIImages or UILabels in a navigation bar :)
Try this code snippet. You can change Label and imageView frame and put as your requirement.
UIView *btn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UILabel *label;
label = [[UILabel alloc] initWithFrame:CGRectMake(5, 25, 200, 16)];
label.tag = 1;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor whiteColor];
label.text = #"My first lable";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 30, 200, 16)];
label.tag = 2;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.adjustsFontSizeToFitWidth = NO;
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.text = #"second line";
label.highlightedTextColor = [UIColor whiteColor];
[btn addSubview:label];
[label release];
UIImageView *imgviw=[[UIImageView alloc]initWithFrame:CGRectMake(170, 10, 20, 20)];
imgviw.backgroundColor = [UIColor blackColor];
imgviw.image=[UIImage imageNamed:#"abc.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
Hope that it helps.
You can do this by not setting any #selector for created button, and setting the same customized properties for both UIControlStateNormal and UIControlStateSelected. Also, set tintColor to ClearColor. Like that, button will still be a button, it will stay there, always look the same but it won't do anything.
Hope it helps.
you can use barButton created with customView.
for example,
UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 100, 40)];
[btn setTitle:#"some Title" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor greenColor]];
self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithCustomView:btn];

Set custom titleLabel properties for multiple UIButtons

I've got a custom button like:
UIButton *catBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
catBtn1.frame = CGRectMake(0, 344, 427, 67);
catBtn1.titleLabel.font = categoryFont;
catBtn1.titleLabel.textColor = [UIColor whiteColor];
catBtn1.titleLabel.frame = CGRectMake(52, 21, 150, 15);
catBtn1.titleLabel.textAlignment = UITextAlignmentLeft;
Is there a quick way to set all these titleLabel properties for all my buttons without doing this for each button? I tried assigning an instance of UILabel to catBtn1.titleLabel but that didn't work...
You can create method for button like this...
-(UIButton *) customBtn:(NSString *)btntxt:(CGFloat)btnx:(CGFloat)btny:(CGFloat)btnwidth
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//[btn setTitle:btntxt forState:UIControlStateNormal];
btn.titleLabel.text = btntxt;
btn.titleLabel.font = categoryFont;
btn.titleLabel.textColor = [UIColor whiteColor];
btn.titleLabel.frame = CGRectMake(52, 21, 150, 15);
btn.titleLabel.textAlignment = UITextAlignmentLeft;
[btn setFrame:CGRectMake(btnx, btny, btnwidth, 40)];
return btn;
}
and you can call it like this any number of time when you need it
[YOURVIEW_OBJECT addSubview:[self customBtn:#"Samanmalliset" : 165:20 :115]];
You can create method that sets these properties and call it for each of your buttons.

Resources