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
Related
I have used the title inset to display the text name under the UIButton.
Here's my code and the output:
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
Here's my expected output:
I have used the UIButton and set the title and UIImage as background. So how to show the title label under the UIButton?
What you're trying to do is correct; you just need to set the inset edges as done below. Then you need to increase your button frame so that it can hold the title and image in the same time.
[button setTitleEdgeInsets:UIEdgeInsetsMake(60, 0, 0, 0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0f, 0.f, button.titleLabel.frame.size.height, 0.f)];
This can also be done via storyboards as #VD Patel suggests that is up to you which approach you prefer.
You could also do this with a separate UIImageview and a button that would be easier to handle and would be more efficient.
Please Select button in xib first. then select Attribute Inspector, in this Select Title in "Edge" and set appropriate "Inset" as per Requirements.
please take a look with below code and set insets as per your Requirements.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, self.view.bounds.size.width, 40);
[myButton setImage:[UIImage imageNamed:#"imageName"] forState:UIControlStateNormal];
[myButton setTitle:#"Rate us on app store" forState:UIControlStateNormal];
[myButton setTitleEdgeInsets:UIEdgeInsetsMake(2, 50, 2, 20)];//set ur title insects myButton
[myButton setImageEdgeInsets:UIEdgeInsetsMake(2, -200, 2, 2)];//make negative edge for left side
[myButton setBackgroundColor:[UIColor greenColor]];
[self.view myButton];
Create an UIImageView,UILabel and UIButton. And later add ImageView and Label as subview to Button.
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 200, 100)];
lbl.textAlignment = NSTextAlignmentCenter;
[lbl setText:#"Mono"];
UIImageView * imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
imgView.image = [UIImage imageNamed:#"your_image.png"];
[imgView setUserInteractionEnabled:NO];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:imgView];
[btn addSubview:lbl];
[btn setFrame:CGRectMake(0, 20, 200, 300)];
[btn addTarget:self action:#selector(OnButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
Try adding this code in viewDidLoad Method.
I am programmatically creating buttons in a view inside UIScrollView. I also have programmatically created UITextField. I can select and enter data into the text fields all day long. But I cannot get any sign the buttons are being touched. I can add them to the main view and they work. But when added to the view inside the UIScrollView, they are lost in another dimension. I've read plenty of info about setting userInteraction, delayContentTouches, and intercepting via gestureRecognizer. The last might be related to my problem. I return NO via [touch.view isDescendantOfView:self.myScrollViewName]. I can't get it to trigger via [touch.view isKindOfClass:[UIButton class]]. Which makes me think I have a more fundamental error I am missing?
related:
https://stackoverflow.com/a/6825839/4050489
edited to add button code per request:
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.myButton.frame = myButtonFrame;
self.myButton.enabled = interface.isActive;
self.myButton.userInteractionEnabled = YES;
self.myButton.exclusiveTouch = YES;
self.myButton.backgroundColor = [UIColor greenColor];
//self.myButton. = YES;
self.myButton.layer.borderWidth=1.0f;
self.myButton.layer.borderColor=[[UIColor blackColor] CGColor];
self.myButton.layer.cornerRadius = 10;
[self.myButton setTag:interface.tag];
[self.myButton setTitle:interface.Name forState:UIControlStateNormal];
[self.myButton addTarget:self action:#selector(navigatePartButtons:) forControlEvents:UIControlEventTouchUpInside];
[self.myButton setEnabled:YES]; //error check
[self.partSetupView addSubview:self.myButton];
partSetupView is a UIView in the UIScrollView.
Try this sample Code:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *myScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 300, 300)];
myScroll.backgroundColor = [UIColor redColor];
[self.view addSubview:myScroll];
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 200)];
myView.backgroundColor = [UIColor yellowColor];
[myScroll addSubview:myView];
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 100)];
myButton.backgroundColor = [UIColor blackColor];
[myView addSubview:myButton];
[myButton addTarget:self action:#selector(btnClick) forControlEvents:UIControlEventTouchDown];
}
-(void)btnClick{
NSLog(#"Button Click");
}
#end
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];
I want to create the view with button, textbox, label programatically in viewdidload
But touch events are not working on view and its subviews even though i have set userinteractionenabled to YES.
Here is my code:
CGRect bounds = [[UIScreen mainScreen] bounds];
//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];
CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventAllTouchEvents];
[myButton setTitle: #"My Button" forState: UIControlStateNormal];
//adding button to myview
[mainView addSubview: myButton];
//adding label to myview
UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=#"my text";
[mainView addSubview:myLabel];
//adding textbox to myview
UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=#"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];
//finally adding my view to self.view
[self.view addSubview:mainView];
try this
//creating a view
UIView* mainView = [[UIView alloc] initWithFrame: bounds];
[mainView setUserInteractionEnabled:YES];
[mainView setBackgroundColor: [UIColor yellowColor]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tappedOnView:)]; //add the gesture to get the touch event
tapGesture.numberOfTapsRequired = 1;
[mainView addGestureRecognizer:tapGesture];
CGRect buttonFrame = CGRectMake( 100, 80, 100, 50 );
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
myButton.frame = buttonFrame;
[myButton setBackgroundColor:[UIColor greenColor]];
myButton.userInteractionEnabled=YES;
[myButton addTarget: self
action: #selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
[myButton setTitle: #"My Button" forState: UIControlStateNormal];
//adding button to myview
[mainView addSubview: myButton];
//adding label to myview
UILabel* myLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
myLabel.text=#"my text";
[mainView addSubview:myLabel];
//adding textbox to myview
UITextField* textbox=[[UITextField alloc]initWithFrame:CGRectMake(100, 210, 200, 50)];
textbox.text=#"my text in textfield";
textbox.userInteractionEnabled=YES;
[mainView addSubview:textbox];
//finally adding my view to self.view
[self.view addSubview:mainView];
modify the line
//UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
to
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
and add the method in your code
-(void)buttonClicked:(UIButton *)button
{
NSLog(#"hai");
}
and select the checkbox enable user interaction in interface builder its working fine.
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];
}