On click of a button a textfield has to be displayed? - objective-c

I have a button and a textfield. Button is just below the textField. Now when I click on the button a new textfield should be displayed below the first text field and the frame of the button should also be changed. When I have tried this it worked for the first time but when I click a second time the new textfield is not displaying and the button frame is also not changing. Help me with this issue.
The code that i have written is:
-(void)onboardCertificatesAddBtnBtnTap
{
int x, y;
if (y =585, x=632) {
_boardCertificationField= [[UITextField alloc]initWithFrame:CGRectMake(5, y+50, self.scrollView.frame.size.width/2-25, 40)];
_boardCertificationField.font = [UIFont systemFontOfSize:17];
_boardCertificationField.borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_boardCertificationField];
_expDateField = [[UITextField alloc]initWithFrame:CGRectMake(220, y+50, self.scrollView.frame.size.width/2-40, 40)];
_expDateField .font = [UIFont systemFontOfSize:17];
_expDateField .borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_expDateField];
_boardCertificatesAddBtn.frame = CGRectMake(5, x+50, 20, 20);
[_boardCertificatesAddBtn setImage:[UIImage imageNamed:#"Add1.png"] forState:UIControlStateNormal];
[_scrollView addSubview:_boardCertificatesAddBtn];
[_boardCertificatesAddBtn addTarget:self action:#selector(onboardCertificatesAddBtnBtnTap) forControlEvents:UIControlEventTouchUpInside];
y=y+50;
x=x+50;
}
else {
_boardCertificationField= [[UITextField alloc]initWithFrame:CGRectMake(5, y+50, self.scrollView.frame.size.width/2-25, 40)];
_boardCertificationField.font = [UIFont systemFontOfSize:17];
_boardCertificationField.borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_boardCertificationField];
_expDateField = [[UITextField alloc]initWithFrame:CGRectMake(220, y+50, self.scrollView.frame.size.width/2-40, 40)];
_expDateField .font = [UIFont systemFontOfSize:17];
_expDateField .borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_expDateField];
_boardCertificatesAddBtn.frame = CGRectMake(5, x+50, 20, 20);
[_boardCertificatesAddBtn setImage:[UIImage imageNamed:#"Add1.png"] forState:UIControlStateNormal];
[_scrollView addSubview:_boardCertificatesAddBtn];
[_boardCertificatesAddBtn addTarget:self action:#selector(onboardCertificatesAddBtnBtnTap) forControlEvents:UIControlEventTouchUpInside];
y=y+50;
x=x+50;
}
}

Declare x and in #implementation ViewController block
like
#implementation Viewcontroller
{
int x,y;
}
and write other code as
-(void)onboardCertificatesAddBtnBtnTap
{
if (y =585, x=632) {
_boardCertificationField= [[UITextField alloc]initWithFrame:CGRectMake(5, y+50, self.scrollView.frame.size.width/2-25, 40)];
_boardCertificationField.font = [UIFont systemFontOfSize:17];
_boardCertificationField.borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_boardCertificationField];
_expDateField = [[UITextField alloc]initWithFrame:CGRectMake(220, y+50, self.scrollView.frame.size.width/2-40, 40)];
_expDateField .font = [UIFont systemFontOfSize:17];
_expDateField .borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_expDateField];
_boardCertificatesAddBtn.frame = CGRectMake(5, x+50, 20, 20);
[_boardCertificatesAddBtn setImage:[UIImage imageNamed:#"Add1.png"] forState:UIControlStateNormal];
[_scrollView addSubview:_boardCertificatesAddBtn];
[_boardCertificatesAddBtn addTarget:self action:#selector(onboardCertificatesAddBtnBtnTap) forControlEvents:UIControlEventTouchUpInside];
y=y+50;
x=x+50;
}
else {
_boardCertificationField= [[UITextField alloc]initWithFrame:CGRectMake(5, y+50, self.scrollView.frame.size.width/2-25, 40)];
_boardCertificationField.font = [UIFont systemFontOfSize:17];
_boardCertificationField.borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_boardCertificationField];
_expDateField = [[UITextField alloc]initWithFrame:CGRectMake(220, y+50, self.scrollView.frame.size.width/2-40, 40)];
_expDateField .font = [UIFont systemFontOfSize:17];
_expDateField .borderStyle=UITextBorderStyleRoundedRect;
[_scrollView addSubview:_expDateField];
_boardCertificatesAddBtn.frame = CGRectMake(5, x+50, 20, 20);
[_boardCertificatesAddBtn setImage:[UIImage imageNamed:#"Add1.png"] forState:UIControlStateNormal];
[_scrollView addSubview:_boardCertificatesAddBtn];
[_boardCertificatesAddBtn addTarget:self action:#selector(onboardCertificatesAddBtnBtnTap) forControlEvents:UIControlEventTouchUpInside];
y=y+50;
x=x+50;
}

Related

Programatically created UIDatepicke done and cancle button is not working

This is my UIDatePicker code but in that picker, done and cancle button is not working ...please help me.... i also add here my done and cancle button methods here..Please give me a proper suggessions for how it works properly.
if (textField.tag==Jobdatetxt.tag)
{
[Jobdatetxt resignFirstResponder];
[data.toolbar removeFromSuperview];
UITextField *lagFreeField = [[UITextField alloc] init];
[self.view.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
//////////////////////////////////////////////// DATE OF JOB////////////////////////////////////////////////
pickerDate = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,result.height - 216, result.width, 216)];
[pickerDate setDatePickerMode:UIDatePickerModeDate];
pickerDate.backgroundColor = [UIColor whiteColor];
[self.view.window addSubview:pickerDate];
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleBlackOpaque;
toolbar.frame=CGRectMake(0,result.height - 266,result.width,50);
cancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancel setTitle:#"Cancel" forState:UIControlStateNormal];
cancel.backgroundColor = [UIColor clearColor];
[cancel.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
cancel.titleLabel.textColor = [UIColor whiteColor];
[cancel addTarget:self action:#selector(CancelClicked:) forControlEvents:UIControlEventTouchUpInside];
cancel.tag = textField.tag;
cancel.frame = CGRectMake(10, 10, 70, 30);
[toolbar addSubview:cancel];
doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneButton setTitle:#"Done" forState:UIControlStateNormal];
doneButton.backgroundColor = [UIColor clearColor];
[doneButton.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
doneButton.titleLabel.textColor = [UIColor whiteColor];
[doneButton addTarget:self action:#selector(DoneClicked:) forControlEvents:UIControlEventTouchUpInside];
doneButton.tag = textField.tag;
doneButton.frame = CGRectMake(result.width - 80, 10, 70, 30);
[toolbar addSubview:doneButton];
[self.view.window addSubview:toolbar];
}
// Done Button method
-(IBAction)DoneClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}
//Cancle Button Method
-(IBAction)CancelClicked:(UIButton *)sender
{
[pickerDate removeFromSuperview];
[toolbar removeFromSuperview];
}

UIButton setTitle Colour Changing not working

I have created scroll view programmatically and created N number of buttons using for loop, but now I need to change setTitle color accordingly the button selected or unselected
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(40,50,self.view.frame.size.width,40)];
_scrollView.backgroundColor = [UIColor whiteColor];
_scrollView.showsHorizontalScrollIndicator = NO;
for (int i = 0; i<_pagesNameArray.count; i++) {
self.button = [[UIButton alloc]init];
self.button.frame = CGRectMake(i*150, 0, 150, 40);
self.button.tag = i;
self.button.backgroundColor = [UIColor clearColor];
[self.button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:[_pagesNameArray objectAtIndex:i] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
[_scrollView addSubview:self.button];
}
[self setupSelector];
CGRect contentRect = CGRectZero;
for (UIView *view in _scrollView.subviews) {
contentRect = CGRectUnion(contentRect, view.frame);
}
_scrollView.contentSize = contentRect.size;
[self.view addSubview:_scrollView];
I also tried this method but, it's not working. Can anyone help me out?
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
-(void)tapSegmentButtonAction:(UIbutton *)sender
{
UIbutton *button=sender.tag;
[self.button setTitleColor:[UIColor blackColor]
forState:UIControlStateSelected];
}
I have created 3 different type of buttons and given tag to each button
self.button = [[UIButton alloc]init];
self.button.frame = CGRectMake(0, 0, 180, 30);
self.button.tag = 0;
self.button.backgroundColor = [UIColor clearColor];
[self.button addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:#"LOCAL REPORTS" forState:UIControlStateNormal];
// [self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
self.button.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.button];
self.buttonNews = [[UIButton alloc]init];
self.buttonNews.frame = CGRectMake(135, 0, 180, 30);
self.buttonNews.tag = 1;
self.buttonNews.backgroundColor = [UIColor clearColor];
[self.buttonNews addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonNews setTitle:#"NEWS" forState:UIControlStateNormal];
//[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.buttonNews.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
self.buttonNews.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.buttonNews];
self.buttonTop = [[UIButton alloc]init];
self.buttonTop.frame = CGRectMake(135*2, 0, 180, 30);
self.buttonTop.tag = 2;
self.buttonTop.backgroundColor = [UIColor clearColor];
[self.buttonTop addTarget:self action:#selector(tapSegmentButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.buttonTop setTitle:#"TOP PHOTOS" forState:UIControlStateNormal];
//[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
self.buttonTop.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
self.buttonTop.titleLabel.font = [UIFont fontWithName:#"Oswald-Regular" size:20.0f];
[_scrollView addSubview:self.buttonTop];
[self animateButton];
-(void) animateButton
{
[self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
if (self.currentIndex == 0) {
[self.button setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
if (self.currentIndex == 1) {
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}
if (self.currentIndex == 2) {
[self.button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonNews setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[self.buttonTop setTitleColor:[UIColor colorWithRed:0.04f green:0.14f blue:0.19f alpha:1] forState:UIControlStateNormal];
}
}

Objective C UIBarButtonItem as toggle button for edit and save

I have written a code to place a right bar button item with customview.While selecting edit it changes to save option.Now I selecting save it saves the values.If again I try to select edit the action not firing.The code I written below,
item = [[UINavigationItem alloc]init];
btnn = [UIButton buttonWithType:UIButtonTypeCustom];
[btnn setFrame:CGRectMake(0, 0, 40, 40)];
btnn.layer.masksToBounds = NO;
btnn.layer.cornerRadius = 8.0;
[btnn addTarget:self action:#selector(rightBarButtonCustomPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnn setImage:[UIImage imageNamed:#"edit"] forState:UIControlStateNormal];
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnn];
item.rightBarButtonItem = rightBtn;
self.navigationBar.items = #[item];
-(IBAction)rightBarButtonCustomPressed:(UIBarButtonItem*)btn
{
item = [[UINavigationItem alloc]init];
btnn = [UIButton buttonWithType:UIButtonTypeSystem];
[btnn setFrame:CGRectMake(0, 0, 40, 40)];
btnn.layer.masksToBounds = NO;
btnn.layer.cornerRadius = 8.0;
[btnn addTarget:self action:#selector(saveBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[btnn setTitle:#"save" forState:UIControlStateNormal];
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnn];
item.rightBarButtonItem = rightBtn;
self.navigationBar.items = #[item];
[self.firstNameText setEnabled:YES];
[self.lastNameText setEnabled:YES];
[self.dateOfBirthTxt setEnabled:YES];
[self.contactNumberlbl setEnabled:YES];
}
-(IBAction)saveBtnClicked:(UIBarButtonItem*)btn {
[self.firstNameText setEnabled:NO];
[self.lastNameText setEnabled:NO];
[self.dateOfBirthTxt setEnabled:NO];
[self.contactNumberlbl setEnabled:NO];
item = [[UINavigationItem alloc]init];
btnn = [UIButton buttonWithType:UIButtonTypeCustom];
[btnn setFrame:CGRectMake(0, 0, 40, 40)];
btnn.layer.masksToBounds = NO;
btnn.layer.cornerRadius = 8.0;
[btnn addTarget:self action:#selector(rightBarButtonCustomPressed:) forControlEvents:UIControlEventTouchUpInside];
[btnn setImage:[UIImage imageNamed:#"edit"] forState:UIControlStateNormal];
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnn];
item.rightBarButtonItem = rightBtn;
self.navigationBar.items = #[item];
I need to achieve barbuttonitem as toggle to save and edit option.Please anybody help me to fix this.Thanks
In place of alloc init evry time , why don't you try changing buttonTitle and then you can perform action on basis of it

Move particular image

I have images under scrollview. Awesomeview having 3 images. When i click button the awesome view go to center. How to move particular image from center of the view. When i use UIPanGestureRecognizer, 3 images are moving. I can't move particular image
code:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 500, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
// CGFloat xOrigin = i * self.view.frame.size.width;
// awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
awesomeView.backgroundColor=[UIColor clearColor];
[scroll addSubview:awesomeView];
imagView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100,100)];
[imagView setImage:[UIImage imageNamed:#"img.png"]];
[awesomeView addSubview:imagView];
[imagView setTag:0];
[imagView setUserInteractionEnabled:YES];
imagView1 = [[UIImageView alloc] initWithFrame:CGRectMake(110, 0, 100,100)];
[imagView1 setImage:[UIImage imageNamed:#"img1.png"]];
[awesomeView addSubview:imagView1];
[imagView1 setTag:1];
imagView2 = [[UIImageView alloc] initWithFrame:CGRectMake(210, 0, 100,100)];
[imagView2 setImage:[UIImage imageNamed:#"img10.png"]];
[awesomeView addSubview:imagView2];
[imagView2 setTag:2];
vc=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scroll addSubview:vc];
UIButton *men = [UIButton buttonWithType:UIButtonTypeCustom];
[men setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateNormal];
// [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];
[men setFrame:CGRectMake(10,0,100,100)];
// [arrowButton addTarget:self action:#selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[vc addSubview:men];
UIButton *departments = [UIButton buttonWithType:UIButtonTypeCustom];
[departments setImage:[UIImage imageNamed:#"img3.png"] forState:UIControlStateNormal];
// [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];
[departments setFrame:CGRectMake(120,0,100,100)];
// [arrowButton addTarget:self action:#selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[vc addSubview:departments];
UIButton *blog = [UIButton buttonWithType:UIButtonTypeCustom];
[blog setImage:[UIImage imageNamed:#"sofa3.png"] forState:UIControlStateNormal];
// [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];
[blog setFrame:CGRectMake(230,0,100,100)];
// [arrowButton addTarget:self action:#selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[vc addSubview:blog];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
}];
Button Click:
-(void)button:(id)sender{
NSLog(#"press");
[myImageView8 addSubview:awesomeView];
}
UIPanGesture:
myImageView8=[[UIImageView alloc]initWithFrame:CGRectMake(20, 150, 170, 150)];
[self.view addSubview:myImageView8];
myImageView8.userInteractionEnabled=YES;
UIPanGestureRecognizer *pan8=[[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(onPan8:)];
[myImageView8 addGestureRecognizer:pan8];
- (void)onPan8:(UIPanGestureRecognizer *)pan {
// CGPoint offset = [pan translationInView:self.view];
CGPoint offset = [pan translationInView:myImageView8];
CGPoint center = pan.view.center;
center.x += offset.x;
center.y += offset.y;
pan.view.center = center;
[pan setTranslation:CGPointZero inView:myImageView8];
}
set ContentOffset value to the scrollview then it automatically moves to the position
[scroll setContentOffset:CGPointMake(320*index, 0) animated:YES];//index is an integer type value

Buttons that aren't a subview of UIView don't work

If I change it to be a subview of timerView it works fine but nothing else works.
timerView = [[UIView alloc] initWithFrame:CGRectMake(0, 278, 105, 33)];
[self.view addSubview:timerView];
UIImageView *changerBackground = [[UIImageView alloc] init];
[changerBackground setImage:[UIImage imageNamed:#"speedbackground.png"]];
changerBackground.frame = CGRectMake(-12 , -16, 105, 33);
[timerView addSubview:changerBackground];
UIButton *normal = [UIButton buttonWithType:UIButtonTypeCustom];
normal.frame = CGRectMake(40, 8, 22, 22);
[normal setBackgroundImage:[UIImage imageNamed:#"normalwhite.png"] forState:UIControlStateNormal];
[normal setBackgroundImage:[UIImage imageNamed:#"normalred.png"] forState:UIControlStateHighlighted];
[normal addTarget:self action:#selector(quickTest:) forControlEvents:UIControlEventTouchUpInside];
[changerBackground addSubview:normal];
[changerBackground setUserInteractionEnabled:YES];
will solve your issue. As the UIImageView disables the user interaction by default.