I've set a UIButton as the titleView of my navigation bar. However the title seems to truncate no matter how wide the button is. Why?
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:[[NSDate date] stringWithNSDateFormatterMediumStyle] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0, 0, 150, 40)];
self.navigationItem.titleView = button;
[button setFrame:CGRectMake(0, 0, 250, 40)]; // are you sure your button is wide enough?
Related
I'm currently making an iOS 8 application ready for the app store upon iOS 8 release, I don't obviously I cannot show much code as I'm keeping the project very quiet but I am creating a view controller with a scroll view and many objects throughout to create a profile for the user, however I'm trying to add a button to all the user to add projects to their profile and the button won't get added to the scroll view... Is there an unwritten rule with UIButtons and UIScrollViews?
Heres the code I'm using to create the button
UIButton *addProject = [[UIButton alloc] initWithFrame:CGRectMake(210, 285, 100, 18)];
[addProject setTitle:#"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:#selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];
Is there something I need to do differently to add it to the scroll view?
EDIT 1: _scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 699);
EDIT 2: Also, the scroll view is created in the storyboard and has a frame of 0, 64, 320, 455 but the content size as above has a larger height
Try with this;
UIButton *addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(210, 285, 100, 18);
[addProject setTitle:#"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:#selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];
Try to add this line;
addProject.backgroundColor = [UIColor redColor];
It's some time happens that it may not appear due to same background color.
UIButton *addProject = [UIButton buttonWithType: UIButtonTypeRoundedRect];
addProject.frame = CGRectMake(210, 285, 100, 18);
[addProject setTitle:#"Show View" forState:UIControlStateNormal];
[addProject addTarget:self action:#selector(addProjectPressed:) forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:addProject];
_scrollView.backgroundColor = [UIColor clearColor];
Hello all I want to customize titleView of navigation bar by adding three buttons on navigation bar in iOS7 below is my code but nothing happens
-(void)addCutomButtonsToNavigationController:(UINavigationController *)navigationController
{
UIView *customNavigationTitleView=[[UIView alloc] initWithFrame:CGRectMake(20, 0, 250, 44)];
[customNavigationTitleView setBackgroundColor:[UIColor blackColor]];
UIButton *friendRequestButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *messagesButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *notificationsButton=[UIButton buttonWithType:UIButtonTypeCustom];
[friendRequestButton setImage:[UIImage imageNamed:#"ic_clients.png"] forState:UIControlStateNormal];
[messagesButton setImage:[UIImage imageNamed:#"ic_location.png"] forState:UIControlStateNormal];
[notificationsButton setImage:[UIImage imageNamed:#"ic_message.png"] forState:UIControlStateNormal];
[friendRequestButton setFrame:CGRectMake(0, 10, 40, 40)];
[customNavigationTitleView addSubview:friendRequestButton];
[customNavigationTitleView addSubview:messagesButton];
[customNavigationTitleView addSubview:notificationsButton];
navigationController.navigationItem.titleView =customNavigationTitleView;
}
Try this
-(void)addCutomButtonsToNavigationController:(UINavigationController *)navigationController
{
UIView *customNavigationTitleView=[[UIView alloc] initWithFrame:CGRectMake(40, 0, 160, 44)];
UIButton *friendRequestButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *messagesButton=[UIButton buttonWithType:UIButtonTypeCustom];
UIButton *notificationsButton=[UIButton buttonWithType:UIButtonTypeCustom];
[friendRequestButton setImage:[UIImage imageNamed:#"ic_clients.png"] forState:UIControlStateNormal];
[messagesButton setImage:[UIImage imageNamed:#"ic_location.png"] forState:UIControlStateNormal];
[notificationsButton setImage:[UIImage imageNamed:#"ic_messsage.png"] forState:UIControlStateNormal];
[friendRequestButton setFrame:CGRectMake(10, 0, 40, 40)];
[messagesButton setFrame:CGRectMake(60, 0, 40, 40)];
[notificationsButton setFrame:CGRectMake(110, 0, 40, 40)];
[customNavigationTitleView addSubview:friendRequestButton];
[customNavigationTitleView addSubview:messagesButton];
[customNavigationTitleView addSubview:notificationsButton];
self.navigationItem.titleView =customNavigationTitleView;
}
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];
I'm trying to add a UIButton in a cell which already has a UIImageView in the background.
I put this code in configureCell
UIButton *mail = [[UIButton alloc] initWithFrame:CGRectMake(10, 270, 40, 40)];
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
mail.backgroundColor = [UIColor whiteColor];
[mail addTarget:self action:#selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:mail];
It works but the image in the UIButton is seems darker
I tried these but it didn't changed anything
mail.alpha = 1;
[cell.contentView bringSubviewToFront:mail];
Does anyone have an explanation for this, and a solution as well.
I had the same problem once and I solved it by setting the button's image for the UIControlStateHighlighted
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateHighlighted];
Hope it works for you
Try this
UIButton *mail=[UIButton buttonWithType:UIButtonTypeCustom];
mail.frame= CGRectMake(10, 270, 40, 40);
[mail setImage:[UIImage imageNamed:#"mail.png"] forState:UIControlStateNormal];
mail.backgroundColor = [UIColor clearColor];
[mail addTarget:self action:#selector(sendMail) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:mail];
I want to create a UIButton that uses its setBackgroundImage, setImage, and setTitle properties, but when I add setTitle in combination with setImage and setBackgroundImage the title doesn't appear.
I've tried the following.
[button setBackgroundImage:[UIImage imageNamed:#"image3.jpg"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"month_pct_general.png"] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"%d",[button tag]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
[button setTitleEdgeInsets:UIEdgeInsetsMake(45, 0, 0, 25)];
your title on button and on title your all image it there so add label on the image like this.
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
UILabel *Label=[[UILabel alloc]initWithFrame:CGRectMake(6, 5, 60, 20)];
[Label setText:#"label"];
Label.backgroundColor=[UIColor clearColor];
[Label setFont:[UIFont fontWithName:#"Helvetica-Bold" size:12]];
Label.textColor=[UIColor whiteColor];
[myButton addSubview:Label];
your all image above the title only so your title is in background so use label on image view.
If you are making GUI using interface builder, then click on the label and go to Editor-> Arrange -> Send to Front.