Tried and searched a lot. Button added as subview is away from view when the frame of superview is small. I dont want it to be appear when frame is small.
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(20 , 100, 200, 30)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:#"OK" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(20 , 100, 30, 20)];
[vw sendSubviewToBack:btn];
[vw addSubview:btn];
[vw setBackgroundColor:[UIColor redColor]];
[self.view addSubview:vw];
this is my code. It look like this
Look on the frame of the UIButton. Its origin.y is 100px. It should be 0, if you want to add it to the vw.
You did wrong. Before adding btn to view, you've called sendSubviewToBack:. Just rewrite as below.
[vw addSubview:btn];//First
[self.view addSubview:vw];//second
[vw sendSubviewToBack:btn];//Third
You don't want to appear if it's lie outside superview, use this. vw.clipsToBounds = YES
I'm trying to understand the following issue.
I create a view like that:
UIView *subNavigation = [[UIView alloc] initWithFrame:CGRectMake(0.0 0.0, 0.0, 70.0)];
Afterwards I'm running through a loop to add buttons to that subNavigation view but I don't want to display the buttons right away:
for (int i = 0; i < 5; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(leftOrigin, 0.0, 145.8, MENU_BUTTON_HEIGHT);
leftOrigin += 145.8;
[button setBackgroundColor:[UIColor whiteColor]];
[button setTitle:#"Button Content" forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
[button setTag:i];
[subNavigation addSubview:button];
}
So the subNavigation was created with a width of 0.0. I do not want to see the buttons when I add the subNavigation but I do. How can I make all subviews adjust to the frame width/height of their superview?
Thank you very much for your help!
I wouldn't mess around with the superview's frame like that if your only intention is to keep its subviews hidden. Instead, I suggest you use [subNavigation setHidden:YES];. This will hide its subviews as well.
If however you are insistent on this approach, I suggest you do as #rokjarc said in his comment and work on the superview's bounds. You should also look into clipping the subviews of subNavigation.
[subNavigation setClipsToBounds:YES];
I'm trying to make a UIButton with an image above the text. To that end, I tried setting the edge insets of the button as so:
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.0f, 10, 0.0f)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(10, 0.0, 0.0, 0.0)];
What I get is a button where the image is in the same place, and the text is squished all the way over to the side, in a space about one character wide.
How can I simply set the insets so I have the image above the text? Is that really so much to ask?
You just wrongly calculated edgeInSets.They are not there the way you think they are. Here's my test code.It did cost me a while to put image and title in the right place.
UIButton *tmp_testBtn = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 60, 40)];
tmp_testBtn.backgroundColor = [UIColor grayColor];
[tmp_testBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 20, 0)];
[tmp_testBtn setImage:[UIImage imageNamed:#"France"] forState:UIControlStateNormal];
[tmp_testBtn setTitleEdgeInsets:UIEdgeInsetsMake(20, -258, 0, 0)];
[tmp_testBtn setTitle:#"testbutton" forState:UIControlStateNormal];
CGRect contentRect = [tmp_testBtn contentRectForBounds:tmp_testBtn.bounds];
NSLog(#"contenRect:%f,%f,%f,%f",contentRect.origin.x,contentRect.origin.y,contentRect.size.width,contentRect.size.height);
CGRect titleRect = [tmp_testBtn titleRectForContentRect:contentRect];
NSLog(#"titleRect:%f,%f,%f,%f",titleRect.origin.x,titleRect.origin.y,titleRect.size.width,titleRect.size.height);
CGRect imageRect = [tmp_testBtn imageRectForContentRect:contentRect];
NSLog(#"imageRect:%f,%f,%f,%f",imageRect.origin.x,imageRect.origin.y,imageRect.size.width,imageRect.size.height);
[self.view addSubview:tmp_testBtn];
[tmp_testBtn release];
By the way, I won't do like this.I prefer customize a button with a UIImageView and a UILabel added on.
I want to display a UIButton in the header section of a UITableView. This is the code I have tried
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 285, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 320.0, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:#"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:headerViewButton];
The problem is I am having a hard time adjusting the button frame, so that it fits fine in the landscape mode as well as portrait mode. I am not very comfortable with using Interface Builder. How can I adjust the button frame correctly so that it fits the view.
I have tried using setAutoresizingMask
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 290, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:#"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth];
[headerView addSubview:headerViewButton];
As the app loads for the first time, the button is missing, but as I change the orientation, the button appears. Every time I try, its the same pattern. The first time the app loads, the button would be missing, and it appears on both orientations after that.
You need to set the button's autoresizingMask to appropriate values. The values depend on how you want the button's size and/or position to adjust as its parent view's size changes.
In the code you posted, it is odd that you are making the button wider than its parent view.
Try setting
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
This way it will be resized as the superview changes. (Works the same as setting autoresizing options in IB)
I need a UIButton with image & text. Image should be in the top & text comes under the image both should be clickable.
I see very complicated answers, all of them using code. However, if you are using Interface Builder, there is a very easy way to do this:
Select the button and set a title and an image. Note that if you set the background instead of the image then the image will be resized if it is smaller than the button.
Set the position of both items by changing the edge and insets. You could even control the alignment of both in the Control section.
You could even use the same approach by code, without creating UILabels and UIImages inside as other solutions proposed. Always Keep It Simple!
EDIT: Attached a small example having the 3 things set (title, image and background) with correct insets
I think you are looking for this solution for your problem:
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setFrame:CGRectMake(0.f, 0.f, 128.f, 128.f)]; // SET the values for your wishes
[_button setCenter:CGPointMake(128.f, 128.f)]; // SET the values for your wishes
[_button setClipsToBounds:false];
[_button setBackgroundImage:[UIImage imageNamed:#"jquery-mobile-icon.png"] forState:UIControlStateNormal]; // SET the image name for your wishes
[_button setTitle:#"Button" forState:UIControlStateNormal];
[_button.titleLabel setFont:[UIFont systemFontOfSize:24.f]];
[_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // SET the colour for your wishes
[_button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; // SET the colour for your wishes
[_button setTitleEdgeInsets:UIEdgeInsetsMake(0.f, 0.f, -110.f, 0.f)]; // SET the values for your wishes
[_button addTarget:self action:#selector(buttonTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside]; // you can ADD the action to the button as well like
...the rest of the customisation of the button is your duty now, and don't forget to add the button to your view.
UPDATE #1 and UPDATE #2
or, if you don't need a dynamic button you could add your button to your view in the Interface Builder and you could set the same values at there as well. it is pretty same, but here is this version as well in one simple picture.
you can also see the final result in the Interface Builder as it is on the screenshot.
Xcode-9 and Xcode-10 Apple done few changes regarding Edge Inset now, you can change it under size-inspector.
Please follow below steps:
Step-1:
Input text and select image which you want to show:
Step-2:
Select button control as per your requirement as shown in below image:
Step-3:
Now go-to size inspector and add value as per your requirement:
swift version:
var button = UIButton()
newGameButton.setTitle("Новая игра", for: .normal)
newGameButton.setImage(UIImage(named: "energi"), for: .normal)
newGameButton.backgroundColor = .blue
newGameButton.imageEdgeInsets.left = -50
In my case, I wanted to add UIImage to the right and UILabel to the left. Maybe I can achieve that by writing code (like the above mentioned), but I prefer not to write code and get it done by using the storyboard as much as possible. So this is how did it:
First, write down something in your label box and select an image that you want to show:
And that will create a button looking like this:
Next, look for Semantic and select Force Right-to-Left (If you don't specify anything, then it will show the image to the left and label to the right like the above image):
Finally, you'll see UIImage to the right and UILabel to the left:
To add space between a label and an image, go to the Size inspector and change those values depending on your requirement:
That's it!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.imageView.image = [UIImage imageNamed:#"your image name here"];
button.titleLabel.text = #"your text here";
but following code will show label above and image in background
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.background.image = [UIImage imageNamed:#"your image name here"];
button.titleLabel.text = #"your text here";
There is no need to use label and button in same control because UIButton has UILabel and UIimageview properties.
Use this code:
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(0, 10, 200, 52)];
[sampleButton setTitle:#"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:#"redButton.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton]
You should create custom imageview for image and custom label for text and you add to your button as subviews. That's it.
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.backgroundColor = [UIColor greenColor];
yourButton.frame = CGRectMake(140, 40, 175, 30);
[yourButton addTarget:self action:#selector(yourButtonSelected:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourButton];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, yourButton.frame.size.width, yourButton.frame.size.height/2)];
imageView1.image =[UIImage imageNamed:#"images.jpg"];
[yourButton addSubview:imageView1];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, yourButton.frame.size.height/2, yourButton.frame.size.width, yourButton.frame.size.height/2)];
label.backgroundColor = [UIColor greenColor];
label.textAlignment= UITextAlignmentCenter;
label.text = #"ButtonTitle";
[yourButton addSubview:label];
For testing purpose, use yourButtonSelected: method
-(void)yourButtonSelected:(id)sender{
NSLog(#"Your Button Selected");
}
I think it will be helpful to you.
Use this code:
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.imageView.frame=CGRectMake(0.0f, 0.0f, 50.0f, 44.0f);///You can replace it with your own dimensions.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0.0f, 35.0f, 50.0f, 44.0f)];///You can replace it with your own dimensions.
[button addSubview:label];
I encountered the same problem, and I fix it by creating a new subclass of UIButton and overriding the layoutSubviews: method as below :
-(void)layoutSubviews {
[super layoutSubviews];
// Center image
CGPoint center = self.imageView.center;
center.x = self.frame.size.width/2;
center.y = self.imageView.frame.size.height/2;
self.imageView.center = center;
//Center text
CGRect newFrame = [self titleLabel].frame;
newFrame.origin.x = 0;
newFrame.origin.y = self.imageView.frame.size.height + 5;
newFrame.size.width = self.frame.size.width;
self.titleLabel.frame = newFrame;
self.titleLabel.textAlignment = UITextAlignmentCenter;
}
I think that the Angel García Olloqui's answer is another good solution, if you place all of them manually with interface builder but I'll keep my solution since I don't have to modify the content insets for each of my button.
Make UIImageView and UILabel, and set image and text to both of this....then Place a custom button over imageView and Label....
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"search.png"]];
imageView.frame = CGRectMake(x, y, imageView.frame.size.width, imageView.frame.size.height);
[self.view addSubview:imageView];
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y,a,b)];
yourLabel.text = #"raj";
[self.view addSubview:yourLabel];
UIButton * yourBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[yourBtn setFrame:CGRectMake(x, y,c,d)];
[yourBtn addTarget:self action:#selector(#"Your Action") forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:yourBtn];
It's really simple,just add image to background of you button and give text to titlelabel of button for uicontrolstatenormal.
That's it.
[btn setBackgroundImage:[UIImage imageNamed:#"img.png"] forState:UIControlStateNormal];
[btn setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[btn setTitle:#"Click Me" forState:UIControlStateNormal];