How to add UIbuttons on UIview programatically - uiview

I have created a view programatically, and want to add 2 button on that view but while running app view, buttons on it are not presented. Here is my sample code, seems problem is with frame but how to adjust button frames according to the frame of view:
cv = [[UIView alloc]initWithFrame:CGRectMake(200, 60, 100, 80)];
UIButton *label1 = [[UIButton alloc]initWithFrame:CGRectMake(100,90, 200, 30)];
label1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[label1 setTitle: #"Mark as unread" forState: UIControlStateNormal];
label1.titleLabel.font=[UIFont fontWithName:#"SegoeUI" size:12.0];
[label1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
label1.backgroundColor=[UIColor blackColor];
[cv addSubview:label1];
UIButton *label2 = [[UIButton alloc]initWithFrame:CGRectMake(-50,20, 200, 30)];
[label2 setTitle: #"Mark as read" forState: UIControlStateNormal];
label2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
label2.titleLabel.font=[UIFont fontWithName:#"SegoeUI" size:12.0];
[label2 addTarget:self action:#selector(nonbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cv addSubview:label2];

First try this code, I have simply removed buttontype setting lines.
UIButton *label1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
//label1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *label2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 40, 100, 30)];
//label2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
Reason: label1 is getting created as per your frame in first line, and in next line, label1 is getting re-assigned to a new button instance created by buttonWithType method. Hence it was getting overridden over first instance where you have already set frames.
Hence your code will become like this:
cv = [[UIView alloc]initWithFrame:CGRectMake(200, 60, 100, 80)];
UIButton *label1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
[label1 setTitle: #"Mark as unread" forState: UIControlStateNormal];
label1.titleLabel.font=[UIFont fontWithName:#"SegoeUI" size:12.0];
[label1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
label1.backgroundColor=[UIColor blackColor];
[cv addSubview:label1];
UIButton *label2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 40, 100, 30)];
[label2 setTitle: #"Mark as read" forState: UIControlStateNormal];
label2.titleLabel.font=[UIFont fontWithName:#"SegoeUI" size:12.0];
[label2 addTarget:self action:#selector(nonbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cv addSubview:label2];

Related

uitextfield is shown but the button is not shown

UITextField is shown but not the UIButton.
How can I show the button ??
UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(30.0, 150, 300, 30.0)];
password.delegate = self;
[password setBorderStyle:UITextBorderStyleRoundedRect];
[password setClearButtonMode:UITextFieldViewModeAlways];
password.placeholder = #"Password";
[password setFont:[UIFont systemFontOfSize:10]];
[self.view addSubview:password];
//button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 200 , 50, 50)] ;
[button addTarget:self action:#selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button = [UIButton buttonWithType: UIButtonTypeSystem];
[self.view addSubview:button];
Try to add BackgroundColor and TitleColor of button:
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
you are intializing button two times.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 200 , 50, 50)] ;
button = [UIButton buttonWithType: UIButtonTypeSystem];
Intialize system button and set its frame later.
UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
button.frame = CGRectMake(30, 200 , 50, 50);
Hope this will help.

Add UIButton to AVCaptureVideoPreviewLayer?

I want to add a UIButton or other type of button to an AVCaptureVideoPreviewLayer.
Here is what I have tried:
[self.view.layer addSublayer:_videoPreviewLayer];
UIButton * sButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 300, 100)];
[sButton setBackgroundColor:[UIColor yellowColor]];
[sButton setTitle:#"Stop" forState:UIControlStateNormal];
[sButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_videoPreviewLayer addSublayer:sLabel.layer];
[_captureSession startRunning];
I also tried something similar with a UIView which was added, even when the AVCapture was not started. I could not however add a subview to that view.
Little late to the party, but I've been working on something similar, so I thought I'd share:
UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(200, 200, 100, 50);
[btn setTitle:#"Hello, world!" forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor blueColor]];
[btn becomeFirstResponder];
UIAccessibilityPostNotification(UIAccessibilityScreenChangedNotification, btn);
[self.view addSubview:btn];

Button in uiview not clickable

Pls help.. i have this button in my subclass, when i addsubview it in my uiview, it cant be clicked, any idea?..thanks
the button in my subclass:
- (void)configureDate{
ChangeButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
ChangeButton.frame= CGRectMake(10, 20, 100, 30);
[ChangeButton setTitle:#"Change Date" forState:UIControlStateNormal];
[ChangeButton addTarget:self action:#selector(changeDate) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:ChangeButton];
}
I added the subclass/button in my uiview:
dateClass = [[DateClass alloc] init];
[dateClass configureDate];
[myView addSubview:dateClass];
[dateClass releaseTable];
the button appears but is not clickable:(
Try this:
[myView addSubview:dateClass];
[myView bringSubviewToFront:dateClass];
Try setting BG color for both dateClass and the ChangeButton button to test.
- (void)configureDate{
ChangeButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
ChangeButton.frame= CGRectMake(10, 20, 100, 30);
[ChangeButton setTitle:#"Change Date" forState:UIControlStateNormal];
[ChangeButton addTarget:self action:#selector(changeDate) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:ChangeButton];
self.backgroundColor=[UIColor redColor];
ChangeButton.backgroundColor=[UIColor yellowColor];
}

iOS : Cannot click the right side of UINavigationItem

-(void )SetNavBarView{
searchBarBtn.hidden=YES;
UIView *NavBarView ;
NavBarView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
btn1.titleLabel.font=[UIFont systemFontOfSize:14];
[btn1 setBackgroundImage:[UIImage imageNamed:#"list.png"] forState:UIControlStateSelected];
[btn1 setBackgroundImage:[UIImage imageNamed:#"nine_grid.png"] forState:UIControlStateNormal];
btn1.tag=10;
[btn1 addTarget:self action:#selector(btnBarClick:) forControlEvents:UIControlEventTouchUpInside];
[NavBarView addSubview:btn1];
UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(45, 0, 44, 44)];
btn2.titleLabel.font=[UIFont systemFontOfSize:14];
[btn2 setBackgroundImage:[UIImage imageNamed:#"map.png"] forState:UIControlStateNormal];
btn2.tag=11;
[btn2 addTarget:self action:#selector(btnBarClick:) forControlEvents:UIControlEventTouchUpInside];
[NavBarView addSubview:btn2];
UILabel *labtitle=[[UILabel alloc]initWithFrame:CGRectMake(95, 0, 120, 44)];
labtitle.text=NSLocalizedString(#"people_nearby", nil);
labtitle.textColor=[UIColor whiteColor];
labtitle.backgroundColor=[UIColor clearColor];
labtitle.textAlignment=UITextAlignmentCenter;
labtitle.font=[UIFont boldSystemFontOfSize:20.0];
[NavBarView addSubview:labtitle];
UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(135, 0, 95, 44)];
btn3.tag=12;
[btn3 addTarget:self action:#selector(btnBarClick:) forControlEvents:UIControlEventTouchUpInside];
[NavBarView addSubview:btn3];
arrowImage=[[UIImageView alloc]initWithFrame:CGRectMake(75, 16, 10, 10)];
arrowImage.image=[UIImage imageNamed:#"arrow_down.png"];//arrow_down.png
[btn3 addSubview:arrowImage];
UIButton *btn4 = [[UIButton alloc] initWithFrame:CGRectMake(230, 0, 44, 44)];
btn4.titleLabel.font=[UIFont systemFontOfSize:14];
[btn4 setBackgroundImage:[UIImage imageNamed:#"ic_action_search.png"] forState:UIControlStateNormal];
btn4.tag=13;
[btn4 addTarget:self action:#selector(btnBarClick:) forControlEvents:UIControlEventTouchUpInside];
[NavBarView addSubview:btn4];
// [bubble stretchableImageWithLeftCapWidth:20 topCapHeight:14]
UIButton *btn5 = [[UIButton alloc] initWithFrame:CGRectMake(275, 0, 44, 44)];
btn5.backgroundColor=[UIColor redColor];
[btn5 setBackgroundImage:[UIImage imageNamed:#"location.png" ] forState:UIControlStateNormal];
btn5.tag=14;
[btn5 addTarget:self action:#selector(btnBarClick:) forControlEvents:UIControlEventTouchUpInside];
[NavBarView addSubview:btn5];
NavBarView.hidden=NO;
// [self.view addSubview:NavBarView];
self.navigationItem.titleView=NavBarView;
}
I have a UINavigationItem , which has five buttons. The fifth button btn5 can only click the left side, but the right of the btn5 cannot be clicked. ( Size of the button is 44 x 44 )
It's because of the size of UINavigationBarItem, it will by default leave a 14px around both sides. If you give a background color to your first button, then you see that space.

How to programmatically add objects/views on a UIScrollView?

I've been using Interface Builder and storyboard to make my application, but for this signature capturing API, everything was done in code.
I'm trying to implement it to my application, but I can't figure out how to add an UIView and Buttons on to my scrollview.
I got it to appear in my view controller, but it isn't connected to the scrollview at all; it appears on top.
Here's my code.
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat padding = self.view.frame.size.width/15;
UIView *autoGraphView = [[[UIView alloc] initWithFrame:CGRectMake(padding, 300, 280, 160)] autorelease];
autoGraphView.layer.borderColor = [UIColor lightGrayColor].CGColor;
autoGraphView.layer.borderWidth = 3;
autoGraphView.layer.cornerRadius = 10;
[autoGraphView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:autoGraphView];
self.autoGraph = [T1Autograph autographWithView:autoGraphView delegate:self];
[autoGraph setLicenseCode:#"4fabb271f7d93f07346bd02cec7a1ebe10ab7bec"];
[autoGraph setShowDate:YES];
[autoGraph setShowHash:YES];
UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// init withFrame:CGRectMake (50, 300, 200, 60)];
[clearButton setFrame:CGRectMake(padding, 480, 130, 30)];
[clearButton setTitle:#"Clear" forState:UIControlStateNormal];
[clearButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[clearButton addTarget:self action:#selector(clearButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clearButton];
UIButton *autoDone = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// initWithFrame:CGRectMake (50, 300, 200, 60)];
[autoDone setFrame:CGRectMake(150 + padding, 480, 130, 30)];
[autoDone setTitle:#"Done" forState:UIControlStateNormal];
[autoDone setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[autoDone addTarget:self action:#selector(doneButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:autoDone];
}
You need to add the subviews to the scrollview, if the root view of your viewcontroller is not the scrollview, you need to get access to it one way or another, typically through an IBOutlet, and add them that way

Resources