Trying to add UIButton on top of vrf-reader ContentView - objective-c

I am trying to insert some buttons in the PDF page rendered by the open source iOs reader vfr-reader https://github.com/vfr/Reader
Basically this is the code I am trying to implement in the initWithFrame method of ReaderContentView:
if (theContentView != nil) // Must have a valid and initialized content view
{
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setFrame:CGRectMake(269, 262, 100, 100)];
[myButton addTarget:self action:#selector(links) forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:#"asdasd" forState:UIControlStateNormal];
[theContentView addSubview:myButton];
The button doesn't work, and I can see that it is rendered and flattened in some way so it doesn't seem to be a button anymore as you can see here at full zoom.
However running DCInspector, despite warning that
2013-05-24 12:27:34.560 Reader[45333:c07] DCIntrospect: *** WARNING: One or more values of this view's frame are non-integer values. This view will likely look blurry. ***
seems to identify the button properly when inspected.
To test this case, just download the reader project and put my code in ReaderContentView at line 113. I am pretty sure I am doing something totally wrong. Thank you for help!

it was a bit tricky but I discovered that userInteractionEnabled was not set properly on the container view. I should have supposed that :)

Related

How to implement a overlay button which is floating over the content view

in some iPhone apps i saw a button which was floating over the content view, eg. in the app EyeEm. When the user is scrolling the content, the button remains where it is and is still an interaction element.
Ho do I implement this?
My approach would be:
Create a view with content
Put a button on it
But how to make the button floating?
edit:
The floating seems to be the default behavior. Interestingly addSubview and insertSubview have the same behavior when placing the button... both are floating over the content.
- (void)addOverlayButton {
UIButton *oButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[oButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
[oButton setTitle:#"Show View" forState:UIControlStateNormal];
oButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:oButton];
//[self.view insertSubview:oButton aboveSubview:_scrollView]; // same result as addSubview.
// Both solutions let the button float over the content.
}
Add a subview to your window's content view with insertSubview:aboveSubview: method with your button and scrollview as arguments, but be careful: if two sibling views both have transparency, the resulting drawing behaviour is undefined.
if you using storyboard just add button over it, like
if you want to do with code insertSubview:buttonView aboveSubview:scrollView
no matters its table or scroll view

UIBarButtonItem image (or UIToolbar custom background image?..) is darker (or lighter?..) than original

I want to create custom buttons for UIToolbar (and a custom background color for UIToolbar) in XCode.
So far, I've done it like this: create UIButton, then set an image to it and then drag it to UIToolbar. It becomes UIBarButtonItem after this, but still it has an image so it's fine for me.
The problem is, that for some reason buttons' images became darker than original images (I can see it because my toolbar background is exactly the same as buttons' background), but when I press the button, I see that its image becomes original-like and then after a sec turns back to the darker one.
I've tried to do as said in this question: Image in UIButton is darker tharn original, setting highlighted image in XCode, but after this it stopped 'changing to original' at all.
So, how can I solve this? I don't really want to bring some GUI stuff like this in code if there's a way to do this by XCode... Any help would be appreciated.
EDIT:
That's the screen shot of what's going on:
And that's how they should look like with toolbar background + buttom image combined:
Now I did it in code like this:
#implementation UIBarButtonItem (CustomView)
+ (UIBarButtonItem*) barItemWithImage:(UIImage *)image target:(id)target action:(SEL)action
{
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* item = [[self alloc] initWithCustomView:button];
return item;
}
#end
And the calling code:
UIBarButtonItem *item =[UIBarButtonItem barItemWithImage:[UIImage imageNamed:#"settings"] target:nil action:nil];
[self.bottomBar setItems:#[item]];
For toolbar background I use this code:
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed:#"toolbar_background"]
forToolbarPosition:UIToolbarPositionAny
barMetrics:UIBarMetricsDefault];
The problem remains the same...
EDIT 2:
Wow, it seems like actually the UIToolbar is lighter than original...
EDIT 3:
Well. Created new project and tested the XCode storyboard-way (with UIButton), there is works fine... That's a mystery for me, totally lost here. Need some serious clarification here about possible reasons of this: my current project is huge and I'm not able to post all the code here...
Well, since nobody answered, I'll just say that I created a new project and the problem gone.
But it happened once again, and I totally don't know why. I did a really lazy and bad thing and changed image brightness for that unlucky button in some image editor =D

UIButton not clickable after UITableView scrolled

I have a UITableView which is populated with some cells. I have created a UIButton using the following snippet, it is placed next to one of the section headers.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton addTarget:self action:#selector(addButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[addButton setBackgroundImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
addButton.backgroundColor = [UIColor clearColor];
addButton.frame = CGRectMake(270, 150, 29, 29);
The button is placed and works correctly. However, after the view is scrolled (even slightly - like 1 pixel), the button works once and then ceases to respond. When it fails to respond the action for when it is clicked is not triggered and the button doesn't give the 'depressed' shadow. The rest of the application runs as normal and it does not crash.
This seems odd because after I scroll the button is clickable once more before it stops working. The button is used to insert rows into the table, so after it is pressed there is an extra row, possibly this is breaking the bounds or something?
Button pressed function:
- (void)addButtonPressed {
self.addClientTable.editing = YES;
// First figure out how many sections there are
NSInteger lastSectionIndex = [self numberOfSectionsInTableView:self.addClientTable] - 1;
// Then grab the number of rows in the last section
NSInteger lastRowIndex = [self.addClientTable numberOfRowsInSection:lastSectionIndex];
[self.addClientTable insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex]] withRowAnimation:UITableViewRowAnimationRight];
self.addClientTable.editing = NO;
}
Where addClientTable is the UITableView.
What could cause a UIButton to stop responding to clicks and where in my scenario would this be caused by?
I am almost sure that your problem is that your button is out of it superview, and you are not using the clip subviews option in your view that contains the button, or in one of it superviews.
Set to true all the views property clip subviews and see if it appears your button. (We expect that the button disappear)
If you provide more code I can try to help you to solve this problem.
-
Reading again your question, another probable problem to it is that you have one view in front of your button. You can test it changing the background of your view, or something like that.

Creating/Displaying Interface Objects with Code for the iPhone [Objective-C]

I'm currently designing an app that calls for dynamically creating objects and then displaying them in a ViewController. My problem is wading through the documentation to try and create the objects I need. I've done some searching and even tried to just sit down and figure it out, but alas, I've got nothing to show for it. I already know how to dynamically create the ViewController but I can't seem to get any objects in it.
Does anyone know how I could go about creating objects (Say a button and a slider) dynamically with Objective-C in XCode?
Creating a button in code is simple
- (void)addButton {
// Create button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// Set the frame (location and size)
[button setFrame:CGRectMake(0, 0, 40, 30)];
// Set what happens when you touch the button
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
// Set button text
[button.titleLabel setText:#"Button"];
// Add button to view
[self.view addSubview:button];
}
A lot of interface elements follow a similar patter- alloc and init the object, set the frame and add it to a view.

EXC_BAD_ACCESS on button press for button dynamically added to UIView within UIScrollView

OK. It's an iPad app. Within the DetailViewController I have added a UIScrollView through IB and within that UIScrollView I have added a UIView (also added through IB) which holds various dynamically added UITableViews, UILabels and UIButtons.
My problem is that I'm getting errors on the UIButton clicks.
I have defined this method:
- (void)translationSearch:(id)sender {
NSLog(#"in transearch");
[self doSearch];
}
This is how I'm adding the UIButton to the UIView:
UIButton *translationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
translationButton.frame = CGRectMake(6, 200, 200, 20);
translationButton.backgroundColor = [UIColor clearColor];
[translationButton setTitle:#"testing" forState:UIControlStateNormal];
[translationButton addTarget:self action:#selector(translationSearch:) forControlEvents:UIControlEventTouchUpInside];
[verbView addSubview:translationButton];
Now the button is added to the form without any issue but, when I press it, I am getting an EXC_BAD_ACCESS error. I'm sure it's staring me in the face but I've passed my usual time limit for getting a bug like this fixed so any help would be greatly appreciated. The only thing I can think is the fact that the UIButton is within a UIView which is within a UIScrollView which is within the view controller is somehow causing an issue.
Cheers.
Thanks Maz. I have just done that and I have to admit that my assumption about where the error was coming from was wrong. It was a new bit of code, just not the button method call.
Grant me the "dunce" award for this one.