I am under one project.In that I created one uibutton at bottom of my viewcontroller using code (Not via storyboard).But when i run my app,my uibutton are placing in differents positions.for example in iphone 5,6 simulator are in some position.In 4s,ipad simulator my uibutton is unvisible.Also i check with my own device(iphone 6) ,in that also my uibutton is not able to see.
Needed
I used below code for creating & placing my button position.but its not fit in one position.
CGFloat buttonSize = 40;
UIButton * disButton = [[UIButton alloc] init];
disButton.backgroundColor = [UIColor redColor];
UIImage *btnImage = [UIImage imageNamed:#"23"];
[disButton setImage:btnImage forState:UIControlStateNormal];
disButton.frame = CGRectMake(150, 523, 40, 40);
[overlayView addSubview:disButton];
[disButton addTarget:self action:#selector(dismissPopUpViewController)
forControlEvents:UIControlEventTouchUpInside];
In my code i use to place position in this line:
disButton.frame = CGRectMake(150, 523, 40, 40);
=> x-position = 150
=> Y-position = 523
Kindly please give some code solution for this problem.
As per all comments about your question.You need to set constraint for your UIButon.Use only centre x,centre y because that will helpfull to see your layout in all devices. And there is a already solution for your problem.see these below links for setting auto layout for UIButton
Link 1
Link 2
Link 3
Sure this will help you !
Related
I've got a Custom Button class that I'm setting up the text for in my storyboard file. The text is a simple "Update Email Address" and does not change throughout the life of the app.
However, I have an image I want to add to the button, and when I do that, I set the image in the following way, in a method inside the common button class:
[self setTintColor:[UIColor whiteColor]];
UIImage *icon = [UIImage imageNamed:iconName];
CGFloat iconWidth = icon.size.width;
[self setImage:icon forState:UIControlStateNormal];
[self setWidth:self.width+iconWidth*2];
[self setOriginX:self.x-iconWidth];
[self setTitleEdgeInsets:UIEdgeInsetsMake(0, -iconWidth*2, 0, 0)];
CGFloat leftMargin = 10;
CGFloat moveLeft = self.titleLabel.frame.size.width + self.titleLabel.x + leftMargin;
[self setImageEdgeInsets:UIEdgeInsetsMake(0, moveLeft , 0, 0)];
However, by setting it this way, it appears the titleLabel's width is 0 so the image isn't being set in the correct location on the button.
My thoughts on why this are happening are potentially because I never set the text programmatically, only in the storyboard, although that really doesn't make sense, it's all I got.
Does anyone see anything wrong here? If you need any other code let me know and I can post up the relevant info.
When do you do this? Maybe you are trying to do this after the resource was loaded but before the frames of all the elements are set.
I'm creating a simple button and adding it as a subview to main view. It doesn't show up though.
This is the code for setting it up:
UIButton* contactButton = [[UIButton alloc] init];
contactButton.titleLabel.text = #"Contact Developer";
[contactButton sizeToFit];
[self.view addSubview:contactButton];
NSLog(#"X: %f || Y: %f || Width: %f || Height: %f", contactButton.frame.origin.x, contactButton.frame.origin.y, contactButton.frame.size.width, contactButton.frame.size.height);
As you may have noticed, I placed a bit of debug code at the end to see the position and dimensions of the button. They are: X: 0, Y: 0, Width: 30, Height: 34
This means it should be showing up in the upper left corner but it doesn't. Whats wrong?
One possible reason for this is that you used titleLabeldirectly. Consider using setTitle:forState: method instead.
To be sure, consider setting backgroundColor, as a debug step, to make sure it's appearing.
Edit As others suggested, try using buttonWithType: instead of [[UIButton alloc] init]. I suggest using UIButtonTypeSystem instead of UIButtonTypeCustom.
You should initialise the button using the following:
UIButton* contactButton = [UIButton buttonWithType:UIButtonTypeCustom];
Try constructing your button a different way:
UIButton* contactButton = [UIButton buttonWithType:UIButtonTypeCustom];
contactButton.backgroundColor = [UIColor redColor];
contactButton.titleLabel.text = #"Contact Developer";
[contactButton sizeToFit];
[self.view addSubview:contactButton];
Are you sure that the button it's not being shown?
I think you are setting the title of the button incorrectly and because of the fact that a default UIButton has clear background and white title, it´s not visible if your superview is white too.
Instead of setting:
contactButton.titleLabel.text = #"Contact Developer"
use:
[contactButton setTitle:#"Contact Developer" forState:UIControlStateNormal]
But you can first try to set a backgroundColor to the button, different that the superview, to see if it's added.
Hope it helps!
(Sorry for my english if i have a mistake)
I am working on an iPad UI and a button on that UI needs to have this image:
http://imgur.com/tVkP8wd
(as a PNG). The button is being declared like this:
CGRect newNoteButtonRect = CGRectMake(0, 0, 69, 43);
UIButton* newNoteButton = [[UIButton alloc] initWithFrame:newNoteButtonRect];
newNoteButton.imageView.contentMode = UIViewContentModeScaleToFill;
[newNoteButton setImage:self.fNewNoteIcon forState:UIControlStateNormal];
where 'fNewNoteIcon' is a UIImage. When the UI comes up, the image is tiny and squished, and almost nothing I do can change that. Any ideas?
The icon is initialized like this:
self.fNewNoteIcon = [UIImage imageNamed:#"New_Note.png"];
In that provided code you haven't specified a UIButton type? Creating a button with a custom image
// Create image
self.fNewNoteIcon = [UIImage imageNamed:#"buttomImage.png"];
// Create rect for button. 0, 0 and size it from image
CGRect newNoteButtonRect = CGRectMake:(0, 0, _fNewNoteIcon.size.width, _fNewNoteIcon.size.height);
// Alloc and init UIButton with type
// Pass in image. Add to subview of view.
UIButton *newNoteButton = [UIButton buttonWithType: UIButtonTypeCustom];
[newNoteButton setImage: _fNewNoteIcon forState: UIControlStateNormal];
[self.view addSubview: newNoteButton];
I type all of this out in SO without compiling so check over but that should get you a UIButton, with the required image at the correct size.
So it looks like if your PNG doesn't have an Alpha channel, then you'll have size issues. I opened the PNG in Photoshop and added an Alpha channel. The image is now properly sized in the iPad!
I have uploaded a custom font and applied this font on the title of a UIbutton using the following code
videoButton.titleLabel.font = [UIFont fontWithName:#"LaurenScript" size:20];
The problem is that the title is being clipped on the top of the first letter (see photo below). I tried the same font on the UIlabel and it works fine so it is not a problem with the font. I tried also to change the rectFrame using
[videoButton.titleLabel setFrame:CGRectMake(0, 0, 300, 600)];
but that didn't do anything.
Has anybody a clue of how I can fix this problem?
Cheers
I had a similar problem, where a diaeresis got cut off on top of the titlelabel.
I made a UIButton subclass and used this code to fix the problem:
-(void)layoutSubviews
{
[super layoutSubviews];
CGRect frame = self.titleLabel.frame;
frame.size.height = self.bounds.size.height;
frame.origin.y = self.titleEdgeInsets.top;
self.titleLabel.frame = frame;
}
Select button in Interface builder and check for set a vertical alignment panel in the control section Below is example:
Not sure if this is still an issue for anyone, but I found that (with using a custom font) the above solutions did not ultimately fix the issue, especially for a custom UIButton created solely programmatically.
Here is how I managed to fix this issue, with 1 line in particular that resolved the clipping issue:
UIButton *button = [[UIButton alloc] init];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Nice!" forState:UIControlStateNormal];
[button setFont:[UIFont fontWithName:<CUSTOM FONT NAME> size:buttonWidth/3.0f]];
button = CGRectMake(0, 0, <WIDTH>, <HEIGHT>);
Here was the line that resolved the clipping:
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];
Hopefully this helps anyone else who still were stuck. Happy coding!
had same problem using a button with an image and text with a custom font.
Everything had to be align centered vertically. And image not stretched.
this worked out fine for me.
btn.contentVerticalAlignment = .fill
btn.contentMode = .center
btn.imageView?.contentMode = .scaleAspectFit
I try this in swift 2.1, I adapt this code from Antoine answer. This may not good code but it solve my problem for now. You should make it better for you self.
import UIKit
class CustomUIButton: UIButton {
override func layoutSubviews() {
if var titleFrame : CGRect = titleLabel?.frame{
titleFrame.size = self.bounds.size
titleFrame.origin = CGPointZero
self.titleLabel!.frame = titleFrame
self.titleLabel!.textAlignment = .Center
}
}
}
There is this (sad) solution: https://stackoverflow.com/a/10200908/352628
I have a similar problem. It seems that the titleLabel is just very uncontrollable, and to get control you need to inject a UILabel subview to the button... That makes me sad :(
I have the following image representing my button:
I want to use that image to create a button that uses that as the background, but that is much wider than the image I supplied.
Here are the two methods that I have tried:
UIButton *emailSupportButton = [[UIButton alloc] initWithFrame:CGRectMake(25, 315, 200, 60)];
[emailSupportButton setTitle:#"Email Support" forState:UIControlStateNormal];
[emailSupportButton setImage:[UIImage imageNamed:#"toolbar-button"] forState:UIControlStateNormal];
This method results in the button image not being stretched and displaying exactly how the .png would display normally.
The other method I have tried is setting the background image, like so...
UIButton *emailSupportButton = [[UIButton alloc] initWithFrame:CGRectMake(25, 315, 200, 60)];
[emailSupportButton setTitle:#"Email Support" forState:UIControlStateNormal];
[emailSupportButton setBackgroundImage:[UIImage imageNamed:#"toolbar-button"] forState:UIControlStateNormal];
This method stretches the image in a really ugly fashion instead of achieving the desired effect, causing the button to be almost elliptical with a really ugly border.
Is the problem that I am not using the right method of creating a custom button, or that my image is not suitable for the task I am trying to accomplish? Is my image supposed to be rectangular and not include the button's border, letting UIButton take care of the border/rounding for me? Is my image supposed to already be the size of the button (this seems a bit limiting)?
The second method you listed (code-wise) is what you want. What you are missing is the stretchable image. Try this:
UIButton *emailSupportButton = [[UIButton alloc] initWithFrame:CGRectMake(25, 315, 200, 60)];
[emailSupportButton setTitle:#"Email Support" forState:UIControlStateNormal];
UIImage *backgroundImage = [UIImage imageNamed:#"toolbar-button"];
CGSize size = backgroundImage.size;
backgroundImage = [backgroundImage stretchableImageWithLeftCapWidth:size.width/2.0 topCapWidth:size.height/2.0];
[emailSupportButton setBackgroundImage:backgroundImage forState:UIControlStateNormal];
If you are deploying on iOS 5 and greater only, then you will want to use the new -[UIImage resizableImageWithCapInsets:(UIEdgeInsets)capInsets]; iOS 6 also adds -[UIImage resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode];