Constraints with multiple subviews - objective-c

My app consists of a deck of cards. Each card itself is created in a class called draggableview. Draggableview has a scrollview, so that the user can scroll through a bunch of images relevant to the card. On the first view, there are buttons, which I created in draggableview.xib and have their methods in the draggableview class. However, as you scroll past the first view, all the other views do not have these buttons.
EDIT:
I've partially solved my problem. In initwithView, I added each button as a subview of draggableview. Now, the buttons appear in all the views of scrollview. In storyboard, I deleted and added new constraints. And in storyboard, the buttons move accordingly. However, in simulator, they're stuck in the right hand corner of the screen.
For this first try, I did this first with one button (review button).
my initWithFrame method:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubviewFromNib];
[self setupView];
panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(beingDragged:)];
cardWidth = frame.size.width;
cardHeight = frame.size.height;
type = 0;
panGestureRecognizer.delegate = self;
panGestureRecognizer.cancelsTouchesInView = NO;
_backgroundScrollView.panGestureRecognizer.cancelsTouchesInView = NO;
likeBadge.image = [UIImage imageNamed:#"likeBadge"];
likeBadge.alpha = 0;
likeBadge.layer.zPosition=99;
passBadge.image = [UIImage imageNamed:#"passBadge"];
passBadge.alpha = 0;
passBadge.layer.zPosition=99;
self.backgroundScrollView.contentSize = self.bounds.size;
[self addSubview: reviewButton];
[self addGestureRecognizer:panGestureRecognizer];
}
return self;
}
How it looks in simulator:

In addition to adding each button as a subview of draggableView in initWithFrame, I also added each button's constraints programmatically. For some reason, this allowed me to move the buttons around (whereas nothing in storyboard was successful) and now they're on all the views of scrollview.

Related

UIViewController's background color becomes transparent when pushing another UIViewController that contains a PDFView [Obj-c]

I'm currently working on a small app that displays a view controller (PDFViewController) that contains a PDFView when a cell in my first view controller (HomeViewController) is clicked. I set the background color of the PDFViewController to be a light gray color in its initializer, but when I push it to my navigation controller, it still appears transparent (I can still see the cells from the HomeViewController).
Here's an image of what's happening. This is the screen shot of when I move the PDFView downward and slightly to the right.
How can I get it so that the view behind this PDFView is just a light gray color?
This is my initializer for PDFViewController:
- (instancetype)init
{
self = [super init];
if (self) {
self.view.backgroundColor = UIColor.lightGrayColor;
// setup PDF view
self.selctedPDF = PDFView.new;
self.selctedPDF.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.selctedPDF];
[self.selctedPDF.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[self.selctedPDF.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;
[self.selctedPDF.topAnchor constraintEqualToAnchor:self.view.topAnchor].active = YES;
[self.selctedPDF.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
}
return self;
}
This is how I'm presenting the PDFViewController:
PDFViewController *viewPDF = [[PDFViewController alloc] init];
viewPDF.selctedPDF.document = [self.pdfs objectAtIndex:indexPath.row].doc;
[self.navigationController pushViewController:viewPDF animated:YES];

Touch UITableView background while scrolling (Kind of an easter egg)

I need to do a hidden button at the bottom of the table view, not in a cell, in the background of the table view itself.
I saw something like this in a game "Where is my water", if you scroll out of bounds you can find a hidden button.
first, I created a simple button, and placed in the bottom of the table view
self.tableView.backgroundColor = [UIColor purpleColor]; //just to see better
UIButton *venom = [[UIButton alloc] initWithFrame:CGRectMake(50.0, self.tableView.contentSize.height+80.0, 100.0, 40.0)];
venom.backgroundColor = [UIColor redColor];
[venom addTarget:self action:#selector(venomAction) forControlEvents:UIControlEventTouchDown];
[self.tableView addSubview:venom];
Because of the self.tableView.contentSize.height+80.0, I need to scroll out of the bounds of the table view to see the button, having something like this:
The result is correct, I want this to be sort of hidden, but the problem is, I cannot click on the button, to see the button, I need to be scrolling and I cannot multitouch.
Can anyone assist me with that? or point me to the right direction
I'd make the button not the tableView's subview, but rather a sibling view above the tableView.
For example, your code could look like this
- (void)viewDidLoad {
[super viewDidLoad];
// Just creating a simple UITableView
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.dataSource = self;
tableView.delegate = self;
self.tableView = tableView;
[self.view addSubview:tableView];
// Creating the Easter Egg Button
UIButton *easterEggButton = [UIButton buttonWithType:UIButtonTypeSystem];
[easterEggButton setTitle:#"Easter Egg Button" forState:UIControlStateNormal];
[easterEggButton addTarget:self
action:#selector(easterEggButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
self.easterEggButton = easterEggButton;
// NOTE that we add the Easter Egg Button to self.view over the Table View, not into Table View
[self.view addSubview:easterEggButton];
}
So we have the button placed over the tableView. This detaches it from interfering with tableView's gestureRecognizer.
Now you need some code to adjust the button's frame to be placed somewhere under the tableView's last cell. It may look something like this:
- (void)updateButtonFrame {
UIButton *button = self.easterEggButton;
// Just the easiest way calculate the width and height of the button depending on it's content
[button sizeToFit];
CGRect frame = button.bounds;
// Now, we want to place the button under all the cells
frame.origin.y = self.tableView.contentSize.height;
frame.origin.y += 60.0f; // add some padding to place it even lower
/* We have calculated button's frame in tableView's coordinate space, so we need to convert it to
* button superview's coordinate space. */
frame = [self.tableView convertRect:frame toView:button.superview];
// And finally apply the frame to the button
button.frame = frame;
}
And of course you need to call this layout method in appropriate moments, that is, whenever tableView reloads it's data (because contentSize may change), and whenever tableView scrolls.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
/* Button resides in self.view and is unaware of Table View scrolling, so we need to update
* it's frame whenever Table View's content offset changes */
[self updateButtonFrame];
}
You may also call it viewDidLayoutSubviews to be safe if tableView's cell height depends on screen orientation and such.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// It's safe to recalculate button's frame whenever self.view layout updates
[self updateButtonFrame];
}
You may have a look at this example implementation
https://gist.github.com/bartekchlebek/429906e05fcbd976291d

Only show the rows in UITableView

I have a UITableView and for the life of me I can't hide the header and all the footer. I have tried multiple methods posted on here.
I want to hide the white space on top and bottom of the populated rows.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Disable Scrolling in TableView
self.tableView.scrollEnabled = NO;
//Get rid of unpopulated rows
self.tableView.tableFooterView = [[UIView alloc] init];
//Hide UITableViewHeader
self.tableView.tableHeaderView = [[UIView alloc] init];
//Populate Table with Test Data
[self testTableView];
}
It looks like your UITableView's layout frame is not getting properly adjusted. Since I am not sure if you are using autolayout constraints or autozingmask therefore please try adding following code to your view controller to force your table to size according to the screen.
- (void)viewDidLayoutSubviews {
self.tableView.frame = self.view.frame;
}
Just set the tableviewHeader to nil or if you want to hide section headers just return nil from viewForHeaderInSection and reload the tableData.

detect long press on UINavigationItem's back button

I want to add functionality to my back buttons through my UINavigationController-based app where long-pressing the back button will pop to root. However, I can't figure out where to attach the gesture recognizer. Do I subclass UINavigationBar and try and detect if the long press is in the left button region?
I've heard of people adding similar functionality before. Anyone have any ideas?
I know this question is old, but I came up with a solution. Instead of trying to add the gesture recognizer to the button itself (which would be ideal), I added it to the self.navigationController.navigationBar and then in the action method, use the locationInView to see if I'm over the back button. I wasn't entirely sure about how to identify the back button precisely, so I'm clumsily just grabbing the the first subview with an x coordinate less than some arbitrary value, but it seems promising. If someone has a better way to identify the frame of the back button, let me know.
- (void)longPress:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
// set a default rectangle in case we don't find the back button for some reason
CGRect rect = CGRectMake(0, 0, 100, 40);
// iterate through the subviews looking for something that looks like it might be the right location to be the back button
for (UIView *subview in self.navigationController.navigationBar.subviews)
{
if (subview.frame.origin.x < 30)
{
rect = subview.frame;
break;
}
}
// ok, let's get the point of the long press
CGPoint longPressPoint = [sender locationInView:self.navigationController.navigationBar];
// if the long press point in the rectangle then do whatever
if (CGRectContainsPoint(rect, longPressPoint))
[self doWhatever];
}
}
- (void)addLongPressGesture
{
if (NSClassFromString(#"UILongPressGestureRecognizer"))
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPress:)];
[self.navigationController.navigationBar addGestureRecognizer:longPress];
[longPress release];
}
}
I believe UIGestureRecognizers can only be added to UIViews and subclasses of UIViews.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIView_Class/UIView/UIView.html
The back button is a UIBarButtonItem that descends from NSObject. Therefore, you won't be able to attach a gesture recognizer to a standard back button using
UILongPressGestureRecognizer *longPressGesture =
[[[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(longPress:)] autorelease];
[self.navigationItem.backBarButtonItem addGestureRecognizer:longPressGesture];
You can however add a custom view to a UIBarButtonItem. A custom view could just as easily be a UIView, UIButton, UILabel, etc.
Example:
UIView *myTransparentGestureView = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,30)];
[myTransparentGestureView addGestureRecognizer:longPressGesture];
[self.navigationItem.backBarButtonItem setCustomView:myTransparentGestureView];
// Or you could set it like this
// self.navigationItem.backBarButtonItem.customView = myTransparentGestureView;
[myTransparentGestureView release];
You have to be careful however, since setting properties on backBarButtonItem applies to the next view that you push. So if you have view A that pushes to view B and you want the gesture to be recognized when you tap back in view B. You must set it up in view A.
I followed a slightly different path, figured I'd share it. The above answers are fine, but really, if the long press is in the leading 1/3 of the nav bar, that's good enough for me:
- (void)longPress:(UILongPressGestureRecognizer *)gr
{
NSLog(#"longPress:");
UINavigationBar *navBar = [self navigationBar];
CGFloat height = navBar.bounds.size.height;
CGPoint pt = [gr locationOfTouch:0 inView:navBar];
//NSLog(#"PT=%# height=%f", NSStringFromCGPoint(pt), height);
if(CGRectContainsPoint(CGRectMake(0,0,100,height), pt)) {
[self popToViewController:self.viewControllers[0] animated:YES];
}
}
Here's my solution:
In appDelegate (the "owner" of the nav bar in my app), In applicationDidFinishLaunchingWithOptions:
Get the nav bar view and add the gesture recognizer to the whole view:
// Get the nav bar view
UINavigationBar *myNavBar = nil;
for (UIView *view in [self.window.rootViewController.view subviews]) {
if ([view isKindOfClass:[UINavigationBar class]]) {
NSLog(#"Found Nav Bar!!!");
myNavBar = (UINavigationBar *)view;
}
}
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:#selector(backButtonLongPress:)];
[myNavBar addGestureRecognizer:longPress];
NSLog(#"Gesture Recognizer Added.");
Then in appDelegate, in -(void) backButtonLongPress:(id) sender
Check to see if the gesture occurs within the frame of the back button:
if ([sender state] == UIGestureRecognizerStateBegan) {
// Get the nav bar view
UINavigationBar *myNavBar = nil;
for (UIView *view in [self.window.rootViewController.view subviews]) {
if ([view isKindOfClass:[UINavigationBar class]]) {
NSLog(#"Found Nav Bar!!!");
myNavBar = (UINavigationBar *)view;
}
}
// Get the back button view
UIView *backButtonView = nil;
for (UIView *view in [myNavBar subviews]) {
if ([[[view class] description] isEqualToString:#"UINavigationItemButtonView"]) {
backButtonView = view;
NSLog(#"Found It: %#", backButtonView);
NSLog(#"Back Button View Frame: %f, %f; %f, %f", backButtonView.frame.origin.x, backButtonView.frame.origin.y, backButtonView.frame.size.width, backButtonView.frame.size.height);
}
}
CGPoint longPressPoint = [sender locationInView:myNavBar];
NSLog(#"Touch is in back button: %#", CGRectContainsPoint(backButtonView.frame, longPressPoint) ? #"YES" : #"NO");
if (CGRectContainsPoint(backButtonView.frame, longPressPoint)) {
// Place your action here
}
// Do nothing if outside the back button frame
}

frame size of detail view of splitviewcontroller does not change up in ios4.2

I am implementing splitviewcontroller with two views master view and detail view in my ipad application. On changing the orientation of ipad from portrait to landscape I am need to hide the master view and change the detail view's frame size to show up on full screen. For this I am using this code.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//adjust master view
UIViewController *master = [self.splitViewController.viewControllers objectAtIndex:0];
UIViewController *detail = [self.splitViewController.viewControllers objectAtIndex:1];
CGRect t = master.view.frame;
t.size.width = 0;
t.size.height = 0;
t.origin.x = 0;
t.origin.y = 0;
[master.view setHidden:YES];
[master.view setFrame:t];
//adjust detail view
CGRect f = detail.view.frame;
f.size.width = 1004;
f.size.height = 768;
f.origin.x = 0;
f.origin.y = 0;
[detail.view setFrame:f];
}
This code works exactly fine on ios3.2 but does not work for ios4.2. The master view gets hidden in ios4.2 but detail view's frame size does not change.
Please help me.
Thanks
Shruti
The alternative that I found to my problem is that instead of hiding the master view and changing the frame size of detail view on rotation I just presented the class containing the detail view as the modal view. Earlier I was pushing it from the previous class. I also added a navigation bar to it with a done button to dismiss the modal view. This thing worked for me.
ListingViewController *viewController = [[ListingViewController alloc] initWithNibName:#"ListingViewController" bundle:[NSBundle mainBundle]];
UINavigationController *modalVC = [[UINavigationController alloc]initWithRootViewController:viewController]; // to add navigation bar
modalVC.navigationBar.barStyle = UIBarStyleBlackOpaque;
[self.navigationController presentModalViewController:modalVC animated:YES];
[modalVC release];
[viewController release];