Why my UIButton doesn't display in my subview? iPad App - ios

Could somebody help me to understand why my UIButton doesn't display in my custom UIView?
In the code below I posted initWithFrame method:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor =[UIColor whiteColor];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, 50)];
toolbar.backgroundColor = [UIColor grayColor];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissNewInvoiceView:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveNewInvoice:)];
[saveBarButton setEnabled:NO];
NSArray *arrayOfItems = [[NSArray alloc]initWithObjects:cancelBarButton,flexibleSpace, saveBarButton, nil];
[toolbar setItems:arrayOfItems];
UILabel *headerViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(335, 15, 200, 20)];
headerViewLabel.text = #"New Invoice";
[toolbar addSubview:headerViewLabel];
[self addSubview:toolbar];
UIButton *selectCustomerButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 60, 60)];
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"];
[self addSubview:selectCustomerButton];
[self bringSubviewToFront:selectCustomerButton];
UILabel *invoiceNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 55, 150, 25)];
[invoiceNumberLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
invoiceNumberLabel.text = #"Invoice number:";
[self addSubview:invoiceNumberLabel];
UITextField *invoiceNumberField = [[UITextField alloc]initWithFrame:CGRectMake(140, 53, 150, 30)];
[invoiceNumberField setFont:[UIFont fontWithName:#"Verdana" size:25]];
invoiceNumberField.placeholder = #"25/13";
[self addSubview:invoiceNumberField];
UILabel *dateOfCreationLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 55, 150, 25)];
[dateOfCreationLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
dateOfCreationLabel.text = #"Date of creation:";
[self addSubview:dateOfCreationLabel];
}
return self;}
Why UILabels are displaying properly by implement addSubview method while UIButtons are not?
Regards

Don't do this:
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"]
Use
[selectCustomerButton setImage:[UIImage imageNamed:#"Farmer.png"] forState:UIControlStateNormal];
The button sets its own image views image depending on its state, you don't access and set the image view's image yourself.

The button will be just invisible in case the image is not loaded/shown properly. As jrturton already said, you should use another method to set the image.
To figure out if the button is 'there', you can give the button a backgroundcolor like this:
selectCustomerButton.backgroundColor = [UIColor redColor];
If you see a red box, you know the image is not properly loaded or maybe not even found.

Related

UIToolbar UILabel x y offset not working

I encountered a strange problem. I need to put two UILabel in UIToolbar. I have a button and I want to share with respect to its center the UILabel, as if I did not change the values of x and y inside UILabel up at the bottom, they do not move. Tell me what could be the problem. I need a little lower down the UILabel.
= [NSMutableArray new];
_createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 100, 20)];
_createdLabel.backgroundColor = [UIColor clearColor];
_createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
_createdLabel.font = myFont;
_createdLabel.text = #"";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:_createdLabel];
[_items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[_items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, -1000, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = #"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(socialSharing:)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[_items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(socialSharing:)];
shareBtn.tintColor = [UIColor whiteColor];
[_items addObject:shareBtn];
[self.toolBar setItems:_items animated:YES];
Please remove [shareLabel sizeToFit]; in your code.
Use below code for share label.
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 4, 85, 16)];
You try this code, it's working fine
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
[toolBar setBarTintColor: self.navigationController.navigationBar.tintColor];
NSMutableArray *items= [NSMutableArray new];
UILabel *createdLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 50, 20)];
createdLabel.backgroundColor = [UIColor clearColor];
createdLabel.textColor = [UIColor whiteColor];
UIFont *myFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
createdLabel.font = myFont;
createdLabel.text = #"Hai";
UIBarButtonItem *createdTitle = [[UIBarButtonItem alloc] initWithCustomView:createdLabel];
[items addObject:createdTitle];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
UILabel *shareLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, 85, 20)];
shareLabel.backgroundColor = [UIColor clearColor];
shareLabel.textColor = [UIColor whiteColor];
UIFont *shareFont = [ UIFont fontWithName:#"GothamPro-Light" size:15.0f ];
shareLabel.font = shareFont;
shareLabel.text = #"Hello "; //#"Поделиться";
[shareLabel sizeToFit];
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(socialSharing)];
[shareLabel setUserInteractionEnabled:YES];
[shareLabel addGestureRecognizer:tap];
UIBarButtonItem *shareTitle = [[UIBarButtonItem alloc] initWithCustomView:shareLabel] ;
[items addObject:shareTitle];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(socialSharing)];
shareBtn.tintColor = [UIColor whiteColor];
[items addObject:shareBtn];
[toolBar setItems:items animated:YES];
[self.view addSubview:toolBar];
provide method like this,
-(void)socialSharing
{
NSLog(#"Hai");
}
Definitely helps to you.

mulpitle UILabel in Navigation titleView [duplicate]

I am developing one application for iphone and in that i have some issues regarding adding more then one UILabel in navigation bar at a specific position.
What i want is in following images
in above image there is one backbar button and one imageview as shown with arrow-mark. Other then that all white box is for difrent UILabels to show in navigation bar.
There is only one UIImageView in the navigation bar and one left bar button. Now problem is that i don't know how to add multiple UILabel in navigation bar at a specific position.
Till now what i have worked is to add only button and UIImageView with the right bar button array or left bar button array, but that only add items in sequnce.
So can anyone please guide me that how can anyone add UILabels or any other item at a specific position..
you can add Multiple UIlabel or Image as look like bellow image:-
this can do using bellow code you can change Label and imageView frame and put as your requirement
- (void)viewDidLoad
{
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:#"a59117c2eb511d911cbf62cf97a34d56.png"];
[btn addSubview:imgviw];
self.navigationItem.titleView = btn;
}
You can add subviews directly to the navigationBar -
UINavigationBar *navBar = navController.navigationBar;
[navBar addSubview: yourLabel];
yourLabel frame origin will be relative to the navigation bar
Try this code
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
buttonContainer.backgroundColor = [UIColor clearColor];
UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
[button0 setFrame: CGRectMake(160, 7 , 40, 30)];
[button0 setTitle:#"Sort" forState:UIControlStateNormal];
button0.titleLabel.font = [UIFont fontWithName:#"Helvetica" size:12];
[button0 setBackgroundImage:[UIImage imageNamed:#"Btn_Sort_Btn_Sort_Places.png"] forState:UIControlStateNormal];
[button0 addTarget:self action:#selector(sortAction) forControlEvents:UIControlEventTouchUpInside];
[button0 setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
self.navigationItem.titleView = buttonContainer;
UILabel *lbl_title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0 , 140, 40)];
lbl_title.text = str_HeaderTitle;
lbl_title.textColor = [UIColor whiteColor];
lbl_title.font = [UIFont fontWithName:#"Helvetica-Bold" size:16];
lbl_title.backgroundColor = [UIColor clearColor];
lbl_title.textAlignment = NSTextAlignmentCenter;
[buttonContainer addSubview:lbl_title];
You can set an arbitrary view instead of a view controller's title:
myViewController.navigationItem.titleView = someView;
Note that most likely the view's frame will be restricted to make room for leftBarButtonItem and rightBarButtonItem.
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(50, 2, 20, 15)];
[label setBackgroundColor:[UIColor greenColor]];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(40, 20, 150, 10)];
[imgView setBackgroundColor:[UIColor redColor]];
[[self.navigationController navigationBar] addSubview:label];
[self.navigationController.navigationBar addSubview:imgView];
Have a look at this piece of code. With some modifications it should solve your problem:
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[imageButton setFrame:CGRectMake(15, 0, 57, 44)];
[imageButton setBackgroundImage:[UIImage imageNamed:#"someImage.png"] forState:UIControlStateNormal];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 0, 250, 44)];
[titleLabel setText:#"some title"];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[self.navigationController.navigationBar addSubview:imageButton];
[self.navigationController.navigationBar addSubview:titleLabel];
Just modify the values in the CGRectMake() to fit your needs.

Adding leftbarbuttons and custom title to navigation bar in iOS

I am adding Custom title and leftbar buttons to navigation bar using following code:-
UIBarButtonItem *button1 =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"img1.png"] style:UIBarButtonItemStylePlain target:self action:#selector(popBack)];
UIBarButtonItem *button2 =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"img2.png"] style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWith:button1, button2, nil];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 360, 30)];
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:14]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:#"Detail Controller"];
label.textAlignment = UITextAlignmentCenter; //NSTextAlignmentCenter
self.navigationItem.titleView=label;
Here, left bar buttons are not been placed properly and title alignment not coming properly(left bar buttons are pushing title to right side of the navigation bar).

Label keeps disappearing from my navigation menu

For some reason the label in my navigation bar keeps disappearing and only displaying my map icon and home button. Is it because I'm adding it as a barbuttonitem? Is there a way around this?
UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(160, 5, 250, 70)];
UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(0,5, 310, 70)];
l1.text=#"FIND THINGS TO DO";
l1.font=[UIFont fontWithName:#"Helvetica Neue LT Std" size:25.0];
l1.backgroundColor=[UIColor clearColor];
l1.textColor=[UIColor whiteColor];
l1.textAlignment=UITextAlignmentCenter;
[view1 addSubview:l1];
UIBarButtonItem *left2=[[UIBarButtonItem alloc]initWithCustomView:view1];
self.navigationItem.rightBarButtonItem=left2;
[self.navigationItem setHidesBackButton:YES];
self.navigationItem.hidesBackButton=YES;
self.navigationController.navigationBarHidden = NO;
UIImage* image3 = [UIImage imageNamed:#"home_btn.png"];
homeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 38, 38)];
[homeBtn setBackgroundImage:image3 forState:UIControlStateNormal];
[homeBtn addTarget:self action:#selector(Home)
forControlEvents:UIControlEventTouchUpInside];
[homeBtn setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *home = [[UIBarButtonItem alloc] initWithCustomView:homeBtn];
NSArray *arr=[[NSArray alloc]initWithObjects:home, nil];
self.navigationItem.rightBarButtonItems = arr;
UIImage* mapButton1 = [UIImage imageNamed:#"Top-Map-Icon.png"];
mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 26, 30)];
[mapButton setBackgroundImage:mapButton1 forState:UIControlStateNormal];
[mapButton addTarget:self action:#selector(Map)
forControlEvents:UIControlEventTouchUpInside];
[mapButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *map = [[UIBarButtonItem alloc] initWithCustomView:mapButton];
NSArray *arr2 = [[NSArray alloc] initWithObjects:map, nil];
self.navigationItem.leftBarButtonItems = arr2;

UIButton inside UITextField

I'm trying to put a UIButton inside UITextfield, to act as a "Done" button to resign the responder. I've tried with the following code, when the field starts editing the done button in the UITextField leftView is visible, however if I select a value from the datePicker or make any text input with the keyboard (in simulator), the done button disappears and the right clearButton is shown.
It seems like it toggles between them both, I changed textFld.LeftViewMode to display always the button but that is not what I'd like...
Thanks in advance!.
- (void)viewDidLoad
{
[super viewDidLoad];
txtFld = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 310, 25)];
txtFld.placeholder = #"__/__/____";
txtFld.font = [UIFont fontWithName:#"HelveticaNeue" size:11];
txtFld.textAlignment = NSTextAlignmentCenter;
txtFld.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
txtFld.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
txtFld.clearButtonMode = UITextFieldViewModeWhileEditing;
txtFld.borderStyle = UITextBorderStyleRoundedRect;
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
doneButton.bounds = CGRectMake(0, 0, 40, 20);
doneButton.frame = CGRectMake(0.0, 0.0, 40.0, 20.0);
doneButton.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:10];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[doneButton addTarget:self action:#selector(resignResponder:) forControlEvents:UIControlEventTouchUpInside];
doneButton.userInteractionEnabled = YES;
// Add button to the UITextField
txtFld.leftView = doneButton;
txtFld.leftViewMode = UITextFieldViewModeWhileEditing;
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[txtFld setInputView:datePicker];
[self.view addSubview:txtFld];
}
-(void)resignResponder:(UIButton *)sender{
UITextField *associatedTxtFld = (UITextField *) sender.superview ;
if ([associatedTxtFld isFirstResponder]) {
[associatedTxtFld resignFirstResponder];
}
}
-(void)datePickerValueChanged:(id)sender{
UIDatePicker *picker = (UIDatePicker *)sender;
NSDateFormatter *formater = [[NSDateFormatter alloc]init];
[formater setDateFormat:#"dd-MM-yyyy"];
txtFld.text = [formater stringFromDate:picker.date];
}
The below code works fine for me . This could be a solution to resign your pickerview
UITextField *dispayNameField = [UITextField alloc]init];
UIButton *userStatusButton = [UIButton buttonWithType:UIButtonTypeCustom];
userStatusButton.frame=CGRectMake(0, 0, 20, 20);
[userStatusButton addTarget:self action:#selector(selectUserStatus) forControlEvents:UIControlEventTouchUpInside];
displayNameField.rightViewMode = UITextFieldViewModeAlways;
displayNameField.rightView = _userStatusButton;
-(void)selectUserStatus
{
displayNameField.inputView = _dataPickerView;
self.editUserProfileScrollView.scrollEnabled = YES;
UIActionSheet *actionSheetForDate = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheetForDate showInView:self.parentViewController.tabBarController.view];
[actionSheetForDate setBounds:CGRectMake(0,0,320, 500)];
UIDatePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 50, 320, 216)];
_dataPickerView.tag = 1;
_dataPickerView.showsSelectionIndicator = YES;
_dataPickerView.dataSource = self;
_dataPickerView.delegate = self;
[_actionSheetForDate addSubview:_dataPickerView];
[_actionSheetForDate sendSubviewToBack:_dataPickerView];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
[pickerToolbar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissActionSheetUserField)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *btnBarCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(dismissActionSheetUserField)];
[pickerToolbar setItems:[NSArray arrayWithObjects:btnBarCancel,flexSpace,doneButton, nil] animated:YES];
[_actionSheetForDate addSubview:pickerToolbar];
displayNameField.inputAccessoryView = pickerToolbar;
}

Resources