UIButton has gap - objective-c

Making a UIButton. for some reason, it has a white gap around it (the grey area is the UIView the button is in):
code:
UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, (THUMBNAIL_SIZE) + (PHOTO_VIEWER_OFFSET_COLUM_1 * 2))];
photoView.backgroundColor = [UIColor lightGrayColor];
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, photoView.frame.size.height)];
myScrollView.backgroundColor = [UIColor lightGrayColor];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 50, 50);
myButton.titleLabel.font = [UIFont fontWithName:labelFont size:16];
[myButton setTitle:#"X" forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:#"add-report.png"] forState:UIControlStateNormal];
myButton.center = CGPointMake(((photoView.frame.size.width - myScrollView.frame.size.width) / 2) + myScrollView.frame.size.width, photoView.frame.size.height / 2);
[photoView addSubview:myButton];
[self.view addSubview:photoView];
I dont' want the white buffers space there, but i can't figure out how to get rid of it.
any ideas?
EDIT: if i try to change the background color:
myButton.backgroundColor = [UIColor orangeColor];
i get this effect:

It might be the text label that has a white background be default?
Try setting every color to [UIColor clearColor] such as button.background and button.titleLabel.background.
Also try to modify the image view. Maybe it does not scale automatically as expected:
button.imageView.contentMode = UIViewContentModeScaleToFill;

Related

ChildView didn't maintain its ParentView Frame

Hope all of you will be fine. Guys i have a situation, I have a uiview and uibutton as under:
UIView* containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, 70)];
containerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:containerView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:#selector(SettingPressed:) forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:#"settings_button.png"];
[button setImage:image forState:UIControlStateHighlighted & UIControlStateNormal & UIControlStateSelected];
button.frame = CGRectMake(0,0, 40, 40);
button.center = containerView.center;
[containerView addSubview:button];
The problem is uibutton appear far from uiview instead of in the middle of uiview. What i am mistaking, please guide me.
Thanks.
First, when you change a view's center property the frame property will be changed automatically so you only should set the button center:
button.center = CGPointMake(containerView.frame.size.width / 2,
containerView.frame.size.height / 2);
Now, whenever you change the center property you need to manually tell the view to redraw itself using setNeedsDisplay :
[button setNeedsDisplay]
and lastly, the difference between frame and bounds that frame defines the position and size relative to the parent view while bounds describes the drawing area inside (or outside) the frame.

Objective-c - Border is over other view

I am trying to put a UILabel over my custom button. The problem is: when I add this label, it is under my button border, I want it over the border.
This is the image of my problem:
(source: abril.com)
.
and this is the code:
//#define DEFAULTCOLOR [UIColor colorWithRed:1 green:161.f/255.f blue:54.f/255.f alpha:1]
UIButton *banner = [UIButton buttonWithType:UIButtonTypeRoundedRect];
banner.frame = CGRectMake(45, 45, 230, 190);
[[banner layer] setBorderWidth:3.0f];
[[banner layer] setBorderColor:DEFAULTCOLOR.CGColor];
[self.view addSubview:banner];
UILabel *overButtonLabel = [[UILabel alloc]initWithFrame:CGRectMake(-10, -10, 168, 20)];
overButtonLabel.backgroundColor = DEFAULTCOLOR;
overButtonLabel.text = #"Testing";
float widthIs = [overButtonLabel.text boundingRectWithSize:overButtonLabel.frame.size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:#{ NSFontAttributeName:overButtonLabel.font }
context:nil].size.width;
overButtonLabel.frame =CGRectMake(-10, -10, widthIs, 20);
overButtonLabel.font = [UIFont fontWithName:#"OpenSans-Bold" size:15];
overButtonLabel.textColor = [UIColor whiteColor];
[banner addSubview:overButtonLabel];
[banner bringSubviewToFront:overButtonLabel];
Regards!
You cannot do that as per CALayer class reference border are always draw above contents:
https://developer.apple.com/library/mac/documentation/graphicsimaging/reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/borderWidth
You can do one thing take a view and add both button and label in that view.
In that view your label must be on the front.

self.navigationItem.titleView cutting off UIBarButtonItem after rotation

I use a self.navigationItem.titleView and I set two UIButtons as the self.navigationItem.rightBarButtonItems in the UINavigationBar. I have achieved this in my navigationBar like so
Whenever I rotate the view controller landscape and then back, the uibarbutton item gets cut off like so
The code I use to set the titleView is this:
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.numberOfLines = 2;
titleLabel.font = [UIFont boldSystemFontOfSize:13.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.textColor = [UIColor whiteColor];
titleLabel.text = self.tabTitle;
self.navigationItem.titleView = titleLabel;
I have no idea why this is happening and I wonder if if any of you know how to fix this.

Position UIBarbuttonItem to the VERY left

I have a UIBarButtonItem that I want hugging the very left of the screen. I've tried putting a fixed barbutton with width=0 before the button and other configurations but there's always several pixels between the button and x=0.
I've also tried changing the imageInset of the UIButton
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.frame = CGRectMake(0, 0, 60, 35);
[moreButton setImage:[UIImage imageNamed:#"Morebutton"] forState:UIControlStateNormal];
moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8);
[moreButton addTarget:self action:#selector(pressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *moreBar = [[[UIBarButtonItem alloc] initWithCustomView:moreButton] autorelease];
But tapping near the edge of the screen doesn't trigger the selector (probably because only the image is moved but not the button itself) and increasing the size of the button doesn't affect the gap between the button and the left side.
Thanks
try to put your button to the container view and change position of button on the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.contentMode = UIViewContentModeScaleAspectFit;
button.frame= CGRectMake(-4.0, 0.0, 30, 30);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cellTextSize.width+10, buttonImage.size.height)] autorelease];
v.backgroundColor = [UIColor clearColor];
[v addSubview:button];
return [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];

iPhone/iPad UIButton TitleLabel text not appearing

I created a grid of buttons. The following code creates the buttons and displays them, but there is no text on the button. Is there a setting I am missing? (Obj-C replies are fine, I'm bi-lingual)
RectangleF frame = new RectangleF (X + 3, Y + 3, cellWidth - 2, cellHeight - 2);
UIButton button = new UIButton (frame);
button.TitleLabel.Text = "Baha'i";
button.TitleLabel.Font = UIFont.FromName ("Helvetica-Bold", 15);
button.TitleLabel.TextColor = UIColor.Black;
button.TitleLabel.Frame = frame;
button.BackgroundColor = UIColor.FromWhiteAlpha(.5f,.5f);
this.AddSubview (button);
I think you want setTitle: forState:
[button setTitle:#"Baha'i" forState:UIControlStateNormal]
A UIButton inherits from UIView, so another way to add text and images, is to add subview. Example:
// My Image
UIImage *buttonImage = [UIImage imageNamed:#"myImage.png"];
UIImageView *buttonImageView = [[[UIImageView alloc]
initWithFrame:CGRectMake(0, 0,
buttonImage.size.width,buttonImage.size.height)] autorelease];
[buttonImageView setImage:buttonImage];
// My Label
UILabel* titleLabel = [[[UILabel alloc]
initWithFrame:CGRectMake(0, 0,
buttonImage.size.width, buttonImage.size.height)] autorelease];
titleLabel.text = #"My Text";
titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 11.0];
titleLabel.textColor = [UIColor blackColor];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
// Create Button with Image and Label
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addSubview:buttonImageView];
[button addSubview:titleLabel];
UIButton * selectCountryBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[selectCountryBtn setTitle:#"My Title" forState:UIControlStateNormal];
Here, I have create sample programmatically button, and then set the title for its Normal Control state, You can use your own button object for set the title string, additionally you can also set title text for another control states by change UIControlStateNormal to another required states.
In my case (Swift 4) it was the following:
btnLogin.setTitle("Welcome, " + var_name, for: [])
For swift Developers-
button.setTitle("Your button Name", for: .normal)
self.ButtonName.setTitle("Here Put Your variable model", for: .normal)
self.Button_name.titleLabel.text = #"Your title name";