adding a button in an UIView - iphone

I'm trying to add a button to a UIview, to cover the whole area of the view.. like this:
-(MBNHomeScreenButton*) initWithImage:(UIImage*)image
andHighlightedImage:(UIImage*)highlightedImage
andTitle:(NSString*)aTitle
withSelector:(SEL)actionButton
forDelegate:(UIViewController*)viewController{
self = [super init];
if (self) {
self.frame = CGRectMake(0, 0, 100, 120);
self.imageView = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage];
self.imageView.center = self.center;
self.imageView.frame = CGRectMake(10, 0, HS_BUTTON_IMAGE_WIDTH, HS_BUTTON_IMAGE_HEIGHT);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.highlighted = NO;
[self addSubview:self.imageView];
self.title = [[UILabel alloc] initWithFrame:CGRectMake(0, HS_BUTTON_IMAGE_HEIGHT, HS_BUTTON_IMAGE_WIDTH + 20, 30)];
self.title.text = aTitle;
self.title.textAlignment = UITextAlignmentCenter;
self.title.textColor = [UIColor whiteColor];
self.title.backgroundColor = [UIColor clearColor];
[self addSubview:self.title];
// This creates a transparent button over the view
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//button.backgroundColor = [UIColor redColor];
[button setFrame:self.frame];
[button addTarget:viewController action:actionButton forControlEvents:UIControlEventTouchUpInside];
[self addSubview: button];
}
return self;}
In the ViewController I create the View like this:
m_newsHSButton = [[MBNHomeScreenButton alloc] initWithImage:[UIImage imageNamed:NEWS_SHADED_IMAGE]
andHighlightedImage:[UIImage imageNamed:NEWS_HIGHLIGHTED_IMAGE]
andTitle:#"News"
withSelector:#selector(newsButtonPressed)
forDelegate:self];
m_newsHSButton.center = CGPointMake(m_backgroundView.frame.size.width / 4, m_backgroundView.frame.size.height / 4);
[backgroundImgView addSubview:m_newsHSButton];
Still... the newsButtonPressed doesn't get called.
Can you help?
Thanks!
====================
[edit]
Ok... figured it out.. Thanks guys.
It was the fact that I was trying to add the view as a subview to a UIImageView with the last line:
[backgroundImgView addSubview:m_newsHSButton];
Apparently, it won't get touches anymore.
Thanks for the effort guys!

Try making the button a #property or instance variable in your custom view. Then call [m_newsHSButton.button addTarget:self action:actionButton forControlEvents:UIControlEventTouchUpInside]; from the viewController.

Related

Trouble Setting Button Image When On a ScrollView

I'm making an app. My app is based off this sample project. https://github.com/yeahdongcn/RSCircaPageControl
In the app there is a view. On the view, there is a scroll view (UIScrollView *scrollView) which allows to 10 "pages" of content. Within each page, there is another scroll view (UIScrollView *sv) which is a subview of *scrollView.
On SV, I then added a button called UIButton *button. When I attempt to change the button's images after a time of "two minutes," nothing happens. This is because there are 10 buttons. I need to change the image of the button on only one of the pages. Not all of them.
Something tells me it needs "object at index" or something to specify which button on which page I want to change.
The code is below! Thanks guys!
- (void)viewDidLoad
{
[super viewDidLoad];
self.clipView = [[RSView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.clipView.clipsToBounds = YES;
[self.view addSubview:self.clipView];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.clipView.bounds.size.width, kScrollViewHeight)];
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.scrollView];
self.clipView.scrollView = self.scrollView;
self.pageControl = [[RSCircaPageControl alloc] initWithNumberOfPages:10];
CGRect frame = self.pageControl.frame;
frame.origin.x = self.view.bounds.size.width - frame.size.width - 10;
frame.origin.y = roundf((self.view.bounds.size.height - frame.size.height) / 2.);
self.pageControl.frame = frame;
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.pageControl setCurrentPage:0 usingScroller:NO];
[self.view addSubview:self.pageControl];
CGFloat currentY = 0;
for (int i = 0; i < 10; i++) {
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, currentY, self.scrollView.bounds.size.width, kScrollViewHeight)];
sv.tag = kScrollViewTagBase + i;
sv.delegate = self;
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
sv.backgroundColor = [UIColor colorWithRed:(arc4random() % 257) / 256. green:(arc4random() % 257) / 256. blue:(arc4random() % 257) / 256. alpha:1];
///here's the buttton!!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage*normal = [UIImage imageNamed:#"girl.jpg"];
UIImage*selected = [UIImage imageNamed:#"girl2.jpg"];
button.frame = CGRectMake(100, 100, 50,50));
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
button.contentMode = UIViewContentModeScaleAspectFit;
[button addTarget:self
action:#selector(myButtonPressed:)
forControlEvents:UIControlEventTouchDown];
[sv addSubview:button]; //notice that the button is added as a subview of SV
[self.scrollView addSubview:sv]; // notice that SV is a subview of the scrollView.
if (i == 2 || i == 6) {
sv.contentSize = CGSizeMake(sv.contentSize.width, kScrollViewContentHeight);
}
currentY += kScrollViewHeight;
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, currentY);
[self performSelector:changeButtomImages withObject:nil afterDelay:TwoMinutes];
}
- (void)changeButtonImages {
UIImage*normal2 = [UIImage imageNamed:#"boy.jpg"];
UIImage*selected2 = [UIImage imageNamed:#"boy2.jpg"];
[button setImage:normal2 forState:UIControlStateNormal];
[button setImage:selected2 forState:UIControlStateHighlighted];
}
Kyle
for setting button image in a normal state the parameter to pass forState: is UIControlStateNormal not UIControlStateHighlighted
So, in changeButtonImages you are changing the the image for the iVar "button", but you are never actually assigning anything to that button var. In your for loop in viewDidLoad, you are creating a local variable of UIButton called button. Maybe something like this?
- (void)myButtonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
UIImage*normal2 = [UIImage imageNamed:#"boy.jpg"];
UIImage*selected2 = [UIImage imageNamed:#"boy2.jpg"];
[button setImage:normal2 forState:UIControlStateNormal];
[button setImage:selected2 forState:UIControlStateHighlighted];
}

UIButton is not responding when added as footerview

My button is not responding to touch events, please find the code snippet as below.
I have added UIButton to UIView and set the UIView to UITableView's footerview.
Please let me know and thanks.
CGRect tblViewFrame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 300);
self.tblViewSettings = [[[UITableView alloc]initWithFrame:tblViewFrame style:UITableViewStyleGrouped]autorelease];
self.tblViewSettings.backgroundColor = [UIColor clearColor];
self.tblViewSettings.showsVerticalScrollIndicator = FALSE;
self.tblViewSettings.delegate = self;
self.tblViewSettings.dataSource = self;
self.tblViewSettings.scrollEnabled = YES;
// Set the background color
self.view.backgroundColor = [UIColor clearColor];
UIView *buttonView = [[[UIView alloc]initWithFrame:CGRectMake(10,10, 320, 60)]autorelease];
self.signoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.signoutButton.frame = CGRectMake(10,10, 300, 40);
UIImage *image = [[UIImage imageNamed:#"btn-large1.png"] stretchableImageWithLeftCapWidth:22 topCapHeight:0];
NSString *strSignOut = NSLocalizedString(#"Sign Out", #"SignOut Button");
[self.signoutButton setTitle:strSignOut forState:UIControlStateNormal];
[self.signoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.signoutButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
[self.signoutButton setBackgroundImage:image forState:UIControlStateNormal];
self.signoutButton.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
[self.signoutButton addTarget:self action:#selector(signOutBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
buttonView.backgroundColor = [UIColor clearColor];
[buttonView addSubview:self.signoutButton];
buttonView.userInteractionEnabled = YES;
//[self.view addSubview:buttonView];
self.tblViewSettings.tableFooterView = buttonView;
self.tblViewSettings.tableFooterView.userInteractionEnabled = YES;
[self.view addSubview:self.tblViewSettings];
-(IBAction)signOutBtnTapped:(id)sender{
}
I have looked at your code and applied it to my project. actually it works. i did not change anything from your code. i guess u must check out the frames of table view and the bottom view as everything else is working fine.
Try capturing the event for UIControlEventTouchDown instead of UIControlEventTouchUpInside in
[self.signoutButton addTarget:self action:#selector(signOutBtnTapped:) forControlEvents: UIControlEventTouchUpInside];
Everything else seems OK.
for Adding button in Footer view Section
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView* customView;
customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];
UIButton *Login_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
[Login_Btn setImage:[UIImage imageNamed:#"btn_login.png"] forState:UIControlStateNormal];
Login_Btn.frame = kiPhoneButtonFrame;
[customView addSubview:Login_Btn];
[Login_Btn addTarget:self
action:#selector(Login_Btn_Clicked:)
forControlEvents:UIControlEventTouchUpInside];
return customView;
}
And Button Click Method
-(IBAction)Login_Btn_Clicked:(id)sender
{
// Code for button CLick which you want to do.
}

UIToolbar is getting hidden

I have created UIToolbar control.which is added as setInputAccessoryView to one UITextField Control.Which works fine. the tollbar comes up with uikeyboard when tapping UITextField.Up to this no issues.
When i placed one button, when tapping on it i want to hide the keyboard and only want to display the toolbar.
This is not happening.the toolbar is getting hidden.
Please let me know , what is the problem/ reason in the code.
Thanks for your time.
- (void)viewDidLoad {
[super viewDidLoad];
//
myToolbar = [[UIToolbar alloc] init];
[myToolbar sizeToFit];
myToolbar.frame = CGRectMake(0,198, 320, 35);
myToolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:myToolbar];
UIImage *image = [UIImage imageNamed:#"orangebuttonsmall.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 100, image.size.width, 25 );
aButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[aButton setTitle:#"Tap" forState:UIControlStateNormal];
[aButton setBackgroundImage: image forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
aButton.titleLabel.font = [UIFont boldSystemFontOfSize:10];
[aButton addTarget:self action:#selector(aCenterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
myToField = [[UITextField alloc] init];
myToField.frame = CGRectMake(0, 0, 320, 48);
myToField.textColor = [UIColor blackColor]; //text color
myToField.font = [UIFont systemFontOfSize:17.0]; //font size
myToField.placeholder = #"To:"; //place holder
myToField.backgroundColor = [UIColor whiteColor]; //background color
myToField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
myToField.keyboardType = UIKeyboardTypePhonePad; // type of the keyboard
myToField.returnKeyType = UIReturnKeyDone; // type of the return key
myToField.clearButtonMode = UITextFieldViewModeWhileEditing;
[myToField setInputAccessoryView:myToolbar];
myToField.textAlignment = UITextAlignmentLeft;
[myToField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
myToField.rightViewMode=UITextFieldViewModeAlways;
// has a clear 'x' button to the right
myToField.delegate = self;
[myToField becomeFirstResponder];
[self.view addSubview:myToField];
}
-(void)aCenterBtnClicked:(id)sender{
[myToField resignFirstResponder];
myToolbar.frame = CGRectMake(0,198, 320, 35);
myToolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:myToolbar];
}
inputAccessoryViews are placed as a subview on the UIKeyboard, if you hide the keyboard, it will hide the inputAccessoryView. Try adding the toolbar as a subview of self.view and animate it's position up when the keyboard is displayed by subscribing to the UIKeyboardWillShow notification and pulling the animation duration from the userInfo.
You try for static toolbar i think that will be easy for you.I am not done before remove from setinputview because there is no method for remove set input view.

Adding a view to UIScrollview

I have created a UIView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *image2 = [UIImage imageNamed:#"pinGreen_v1.png"];
UIImage *image1 = [UIImage imageNamed:#"PinDown1.png"];
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[self addSubview:btn1];
for(int i =1; i<20; i++){
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(40 * i, 40, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
}
return self;
}
With the above code I am drawing images in my view. I am loading this view from my view controller. I need to put this view inside a UIScrollview. Where should I create the scroll view? In the above view or the view controller where the above view is created.
The code inside my view controller is :
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[drawLines release];
I should be able to scroll through the images inside my view.
I would create the the scrollview in the viewcontroller and add the view to it.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[scroll addSubview:drawLines];
scroll.contentSize = CGSizeMake(self.view.frame.size.width*drawLines.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[drawLines release];
[scroll release];

iOS: How to remove overlay from it's subview?

I have button that makes overlay with label and X button on it:
- (IBAction)taptaxi:(id)sender {
UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
overlay.backgroundColor = [UIColor colorWithWhite:0 alpha:.7];
UILabel *waiting = [[[UILabel alloc] initWithFrame:CGRectMake(50, 210, 250, 30)] autorelease];
waiting.text = #"Waiting for cab response...";
waiting.textColor = [UIColor whiteColor];
waiting.backgroundColor = [UIColor clearColor];
waiting.font = [UIFont systemFontOfSize:14];
UIButton *stoprequest = [[UIButton alloc] initWithFrame:CGRectMake(225, 219, 13, 13)];
UIImage *srbackground = [[UIImage imageNamed:#"iks.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0];
[stoprequest setBackgroundImage:srbackground forState:UIControlStateNormal];
[stoprequest addTarget: self action: #selector(stopRequestMethod:) forControlEvents: UIControlEventTouchUpInside];
[self.view.window addSubview:overlay];
[overlay addSubview:waiting];
[overlay addSubview:stoprequest];
}
- (void)stopRequestMethod: (id)sender
{
}
Question is, how to hide/remove overlay with label and button when I tap on X butoon (stopRequestMethod)?
Give your overlay a tag:
overlay.tag = 42;
Then, in your method that gets called when the X button is pressed:
UIView *overlay = [self.view.window viewWithTag:42];
[overlay removeFromSuperview];