Place floating UIButton on top of UICollectionView - ios

I'm desperately trying to place a UIButton on top of my UICollectionView. The goal is something like the check-in button from the new foursquare app.
My code currently looks like so:
UIButton *floatingButton = [[UIButton alloc] init];
[floatingButton setFrame:CGRectMake(320, 150, 50, 50)];
[floatingButton setBackgroundColor:[UIColor blackColor]];
[self.collectionView addSubview:floatingButton];
[self.collectionView bringSubviewToFront:floatingButton];
The problem is the button isn't anywhere.

you probably want to add the button to the collectionView's parent view.
most likely self.view, but its hard to say for sure without seeing more code.
instead of
[self.collectionView addSubview:floatingButton];
you probably want
[self.view addSubview:floatingButton];

I had the same problem. Add your button to collectionView superView, might be self.view.
[self.view addSubview:floatingButton];
and set the button as firstResponder.
[floatingButton becomeFirstResponder];

Related

UIButton click event crash after adding the viewcontroller's view as a sub view

I want to add several ViewController's views as sub views of my another viewcontroller. So I have done like this inside my parent viewController.
-(void)MakeDisplayArea
{
vwDisplayArea=[[UIView alloc] initWithFrame:CGRectMake(0, 0, w, h-scrolTab.frame.size.height)];
[self.view addSubview:vwDisplayArea];
}
-(void)ProfClick :(id)sender
{
[self MakeDisplayArea];
ProfileViewController *profviewcontroller=[[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
profviewcontroller.view.frame=vwDisplayArea.frame;
[profviewcontroller.view setBackgroundColor:[UIColor clearColor]];
[vwDisplayArea addSubview:profviewcontroller.view];
}
Then inside the profViewcontroller ViewDidLoad methods I am generating a button and set the target like this.
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(10, 60, 100, 30)];
[btn setTitle:#"Button Test" forState:UIControlStateNormal];
[self.view addSubview:btn];
[btn addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)btnClick :(id)sender
{
NSLog(#"button clicked-------");
}
I can see the button is appearing on that profViewcontroller that I loaded as a sub view, but when I click the button the app is crashing. What is the correct way add that viewcontroller as a subview on my first viewcontroller. Please help me.
Thanks
Like maddy said you need child view controllers to add working subviews.
Look here: Creating Custom Container View Controllers
But working with them is not easy. Ask yourself if you really need to implement it. For the most cases you don't need it.

NavigationItem LeftBarButtonItem Custome View Auto Layaut

I am trying to set a costume navigationItem.leftBarButtonItem from a nib, but i get an weird behaviour in landscape.
EDIT:
To be clear the view contains a button and a label,this is why i am using a costume view, both button and label have constraints.
self.buttonView =//init;
[self.buttonView setFrame:CGRectMake(0, 0, 36, 36)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.buttonView];
[self.buttonView setBackgroundColor:[UIColor redColor]];
//you will see why
Seems that autolayout is trying to satisfy some constraints thus its expanding your button's frame.
To fix this set the contentHugging priority to required for your button:
[self.buttonView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
Try that and let me know if it works
This is the only workaround i could find asap, if someone have a better idea please feel free to share
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
[self setMenuButtonFrame];
}
-(void)setMenuButtonFrame
{
self.menuView.frame = CGRectMake(self.menuView.frame.origin.x,
self.menuView.frame.origin.y,
self.navigationController.navigationBar.frame.size.height-8,
self.navigationController.navigationBar.frame.size.height-8);
}

display the view behind tableView (or scrollView)

For example:
there is a tableView, when it is pulled down, the back view display. the effect is like this:
I tried like this but not working:
[self.view addSubview:logoView];
[self.view addSubview:tableView];
You have to
add your logo image view below the table view in xib.
set table view background color to clear color
Set logo image hidden.
in - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate or check for your self which delegate is best suited. set the logo image hidden to NO
You set the tableView background as your logoview. Keep your UITableviewCell as what color you want.
I'm not sure if i recognize your problem but i think you want something like this:
you'll have to use:
[self.view insertSubview:aboveSubview: ];
[self.view insertSubview:belowSubview: ];
not
[self.view addSubview: ];
You can achieve the same as the example image above by adding the view with a negative y offset.
UIView *header = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f - self.view.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height)];
header.backgroundColor = [UIColor lightGrayColor];
[self.tableView addSubview:header];
However if you have content in this view, such as a logo, it will appear to be pulled down from the top, rather than behind the table, I don't know if this is the effect you want or not.

Changing programatic uiscrollview to IB

got a bit of a small problem here. I've been following this tutorial which creates an automatic scrolling slideshow using uiscrollview. This piece of code is used in my viewdidload -
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 120, 320, 180)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 280, 320, 60)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
As you can see a small uiscrollview is created programatically which is 320x180. It works perfectly except for the fact that my UI has another uiscrollview which takes up the whole ui, which is around 320x700. I need to embed this programatically created scrollview into it, any ideas? Or alternatively I need to create my own scrollview using storyboards and link it across using code, but have no idea how I would go about this.
Any ideas would be much appreciated.
You have to embed the scroll view inside the scroll view not the main view
[self.scrollView addSubview:scr];

Table footer view buttons

I have some signup form that had email and login text fields as table cells and signup button as button in footer view.
That all functioned superb, here is the code
frame = CGRectMake(boundsX+85, 100, 150, 60);
UIButton *signInButton = [[UIButton alloc] initWithFrame:frame];
[signInButton setImage:[UIImage imageNamed:#"button_signin.png"] forState:UIControlStateNormal];
[signInButton addTarget:self action:#selector(LoginAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.hidesBackButton = TRUE;
self.tableView.tableFooterView.userInteractionEnabled = YES;
self.tableView.tableFooterView = signInButton;
self.view.backgroundColor = [UIColor blackColor];
My question is how to add another button next to this one.
By using [self.tableView.tableFooterView addSubview:...] buttons are not shown and if I use some other UIView, place buttons there and then say that the footerView is that UIView, I see the buttons, but am unable to press them.
I hope that this isn't too confusing and that you understand my problem.
Take a look at this question: UIButtons on tableFooterView not responding to events
your first try is wrong, if you are doing as you as saying you are trying to add a button to the subview of a button:
first you
...
self.tableView.tableFooterView = signInButton;
...
and then later
...
[self.tableView.tableFooterView addSubview:...]
...
but tableFooterView is signInButton. So that is why that is not working.
your second try is correct and the answer yonanderson pointed you should work out and is the correct way to do this, you just need to :
[yourButtonsView addSubView:button1];
[yourButtonsView addSubView:button2];
yourButtonsView.userInteractionEnabled = YES;
self.tableView.tableFooterView = yourButtonsView;
self.tableView.tableFooterView.userInteractionEnabled = YES;
Humm, I didn't read this close enough before answering. I think you need to set the container UIView's userInteractionEnabled property then as you tried, set the footerView to the container with the subviews.
It took me forever to figure out why my footer's buttons were not calling the designated actions. Finally I discovered that I needed to adjust my footer's height. This fixed it for me:
-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return footerView.bounds.size.height;
}

Resources