Custom background resulting in custom button not showing - ios

I set my custom background in my AppDelegate.m:
[self.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]]];
And in my viewcontroller i make my custom button before adding it as a subview:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.mainButton setImage:[UIImage imageNamed:#"aButton"] forState:UIControlStateNormal];
[self.mainButton setImage:[UIImage imageNamed:#"aButton"] forState:UIControlStateHighlighted];
[self.mainButton setFrame:CGRectMake(100, 100, 300, 100)];
[self.view addSubview:self.mainButton];
}
Only my background is showing. If I remove the background - the button appears.

[super viewDidLoad];
// Do any additional setup after loading the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"aButton"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"aButton"] forState:UIControlStateHighlighted];
[button setFrame:CGRectMake(100, 100, 300, 100)];
[self.view addSubview:button];
And do not forget the extention "aButton.png" "aButton.jpg"...

Set a pattern for UIViewController or just write a line
[self.View bringSubViewToFront:_button]

Related

Adding two button via coding in viewDidLoad section is always hiding one button

Adding two button via coding in viewDidLoad section is always hiding one button, and I don't know why this is happening.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
//array initialization
list=[[NSMutableArray alloc]init];
heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
heading.text=#"Lists";
heading.backgroundColor=[UIColor clearColor];
[self.view addSubview:heading];
UIImage *addButton=[UIImage imageNamed:#"add.png"];
add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
[add addTarget:self action:#selector(addList) forControlEvents:UIControlEventTouchUpInside];
[add setImage:addButton forState:UIControlStateNormal];
[self.view addSubview:add];
UIImage *contact=[UIImage imageNamed:#"contacts.png"];
UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
[add addTarget:self action:#selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
[add setImage:contact forState:UIControlStateNormal];
[self.view addSubview:cntctBtn];
}
This looks like a copy and paste error. You never set the frame, image, or target action for the second button because you have "add" where you should have "cntctBtn".
- (void)viewDidLoad {
[super viewDidLoad];
//array initialization
list=[[NSMutableArray alloc]init];
heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
heading.text=#"Lists";
heading.backgroundColor=[UIColor clearColor];
[self.view addSubview:heading];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addButton.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
[addButton addTarget:self action:#selector(addList) forControlEvents:UIControlEventTouchUpInside];
[addButton setImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
[self.view addSubview: addButton];
UIImage *contact=[UIImage imageNamed:#"contacts.png"];
UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cntctBtn.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
[cntctBtn addTarget:self action:#selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
[cntctBtn setImage:contact forState:UIControlStateNormal];
[self.view addSubview:cntctBtn];
}
Its a copy paste error. Above is the corrected code.

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];

UIScrollView error in iOS

I am new to UIScrollView. I don't know how to add buttons with them. In my app, I need a button in horizontal view. But when I tried to create the UIScrollView programatically, its not working. It is throwing some errors. pls help me
I tried to implement this using storyboard too, but failed as I dont how to connect them both using the viewDidLoad.
this is the code I have used so far:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.layerPosition = self.toplayer.frame.origin.x;
UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollview.showsHorizontalScrollIndicator=YES;
scrollview.userInteractionEnabled=YES;
scrollview.scrollEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Excavations",#"Concrete",#"Framing",#"Insulation" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);
}
if I am doing wrong pls tell me the correct procedure to do it.
thanks
I can see just one error in your code now:
[button setTitle:#"Excavations",#"Concrete",#"Framing",#"Insulation" forState:UIControlStateNormal];
setTitle: expect just one string, try this:
[button setTitle:#"Excavations" forState:UIControlStateNormal];
You probably want to add this button thou your scroll view so you need to call:
[scrollview addSubview:button];
It should help, if not post your error.
I assume you want to pass UIControlEventTouchUpInside instead UIControlEventTouchDown as a button control event.
try this .it will work
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.layerPosition = self.toplayer.frame.origin.x;
UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollview.showsHorizontalScrollIndicator=YES;
scrollview.userInteractionEnabled=YES;
scrollview.scrollEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Excavations" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);
}

ACTION for UIBUTTON in iOS

I have one custom class with subclass of UIView, inside this I created one button dynamically.
Now I want to set Action for button method. I set normal like UIviewController.
But its not working.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *but=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:but];
// Initialization code
}
return self;
}
-(void)aMethod
{
NextEyeViewController *nexteye=[[NextEyeViewController alloc]initWithNibName:#"NextEyeViewController" bundle:nil];
[self presentViewController:nexteye animated:YES completion:nil];
}
-(void)aMethod is my method name for button
If you want to set an action to your button created programmatically you need to do like this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 160.0, 40.0);
[view addSubview:button];
Create UIButton:
UIButton *yourButton = [[UIButton alloc] initWithFrame:frameButton];
// Here code to set button properties: color, label...
[yourButton addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:yourButton];
Your method has to receive the sender of the action:
-(void)aMethod:(id)sender {
// your code
}
Update:
You also have to call your method suitably with #selector(aMethod:) instead of #selector(aMethod).

Toggle Button Image

I have multiple of buttons.
They all load in the viewDidLoad. I want it so once one button has been pressed, it changes to the other image.
So Img1 loads via the viewDidload, then once it's been pressed, it goes to Img2
This is my current code which i have tried:
In the viewdidload:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
In the ibaction (which i tried, but im guessing)
-(IBAction)ButtonAction
{
if (btn1==0)
{
btn1=1;
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
}
else
{
[btn1 setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateNormal];
btn1=0;
}
}
Thanks for the help
You need to make sure you are hooking up your UIButton to the proper selector. Also, I would use an #property or a global / instance variable to track which image is currently in the UIButton. Here's some code that should do the trick:
//... in #interface
#property (nonatomic, strong) NSString *btn1ImageName;
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.btn1ImageName = #"img1.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"img1.png"]) ? #"img2.png" : #"img1.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
if (btn1==0)
I don't get that part.
One easy solution would be to have two different images for a selected and un-selected state.
btn1.selected = YES, will automatically choose the selected button image.
btn1.selected = NO, will automaticall choose the un-selected button image.

Resources