Custom UIView for HTML element in UIWebView - iphone

I would like to add a custom view to the layout of an UIWebView, so that the HTML/JS/CSS describe the layout of the "page" with my custom controls in it. Ideally, it would be possible to place e.g. a special element that triggers a callback into my code that would allow me to place my own UIView derivative on the specified coordinates (and it would of course scroll and zoom with the parent UIWebView) and with the specified set of parameters.
A poor man's choice would be something similar to this question:
Getting the position of an HTML element in UIWebView however I am not sure how reliable that would be w.r.t layout changes.

Well, in addition to getting to coordinates from the SubView as described in your link, the only other trick is to attach to UIWebView's ScrollView, which happens to be the subview of it, usually number 0 unless you've done something odd. Then your view will scroll and zoom with the rest of your webpage. Assuming you get these pieces to work correctly, you should post your resulting code. It'd be interesting to see.
For example, attaching a button at 150, 200
//Make a button
UIButton * myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(150, 200, 50, 30 );
myButton.titleLabel.text = #"Press me";
[myButton addTarget:self action:#selector(pressed:) forControlEvents:UIControlEventTouchUpInside];
[[[self.myWebView subviews] objectAtIndex:0] addSubview:myButton];
Although for robustness, you might want to verify that self.myWebView has a subview and that it is of a UIScrollView class before adding to it.

Related

How to add button to MBProgressHUD, relative to text label?

I want to add a custom cancel button to MBProgressHUD, and I want it to be just right of text label. But MBProgresHUD is drawn by overriding drawRect method, so, while I can add button as subview to HUD, i don't know how large text label will be so I can position my button properly.
How can this be achieved?
I'm not sure this is possible directly using MBProgressHUD. Not without rewriting much of it.
One option could be to just add your own button as a subview of the view you are adding the HUD to that would just close the HUD or cancel the action.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 50)]; // made up coords here
// set button stuff here
[self.view addSubview:button];
You will need to set the button actions, background or image (for look and feel), then add it to your view. Basically 2 views to achieve what you need. HUD and button. Probably a lot easier than rewriting MBProgressHUD to allow buttons.

Create Custom button like human Hand

As a part of my app i need to fire a method while clicking any where inside a human hand image. Human hand image is placed in a image view. i need to add a button in-spite of adding gestures. Is it possible to create a custom button like human hand exactly show in the image below?
thanks
Create a custom UIButton add the image hand as its backgroundImage or setImage. Then use /assign this image inside. Like below:
handButton = [UIButton buttonWithType:UIButtonTypeCustom];
[handButton addTarget:self action:#selector(handImage_touch) forControlEvents:UIControlEventTouchUpInside];
[handButton setBackgroundImage:[UIImage imageNamed:#"handIMAGE.png"] forState:UIControlStateNormal];
handButton.frame = CGRectMake(x, y, width, height);
[self.view addSubview:handButton];
-(void)handImage_touch{
// do anything
}
I think at first you have to make polygon which fit to your image. And then you can use touchesBegan:withEvent: to get the coordinate of touch point and judge whether the point is inside of polygon or not.
Here is similar question like yours.
How to get particular touch Area?
I think this is a little difficult work, so maybe you would better use cocos2d library which have collision judgement function.
http://box2d.org/forum/viewtopic.php?f=9&t=7487
But also I think iOS is well constructed for handling touch, so this is beneficial effort for you.

IOS Custom UIButton

I currently work on a small application in which I have several UIButtons. Each of them has an UIImageView beneath it. At the moment, I associate each UIButton with each UIImageView (all are named via an IBOutlet - UIButton and UIImageView). When I press a button, the underlying image changes. I would like to know if it's possible (and how) to create a button which contains an underlying UIImage (not a UIButton image or background image), a sort of new object.
This image is slightly larger than the associated UIButton, like the apple calculator basic apps, and this image changes when I press the button.
I've tried several solutions (certainly incomplete) and I'm beginning to despair.
My current solution is functional but I do find it not particularly elegant.
UIButton has both image and backgroundImage as its properties.
To achieve what you are looking for, I would actually recommend you to stick to just a UIButton, and then using contentInset to position the image over the backgroundImage so it creates the effect I think you are looking for. This should be quite easy in interface builder...
you can make an invisible button and behind it make the image..
This will give you what you want but should be avoided at cost...
Stick to UIButton..
I believe even in calculator app they are only UIButtons and nothing else..
they have been coded to been momentary selected ..so that's why you see them kind of changing highlight for a small fraction and each button have been coded to perform specific function(+ - / equal .)
Update : i might be in doubt but if you asked for a button to have an image view inside it..
then here is the answer.
Make UIbutton. Make an ImageView and then call [theButton.view addSubview:ImageView];
Since both UIbutton and UIImage view inherit from UIView.. you can add as many subviews to them..that is inside them
If I understood your question correct then you may try the following:
UIButton *button = [UIButton alloc]init];
[button setImage:buttonImage forState:UIControlStateNormal]; when button not pressed
[button setImage:buttonImage forState:UIControlStateSelected]; when button tapped
[button release];

Add UIStuff in an UIView

I want to add my buttons in a UIView, like then I can hide or not them.
My button's code:
carte1J1=[UIButton buttonWithType:UIButtonTypeCustom];
carte1J1.tag=11;
carte1J1.frame=CGRectMake(60, 240, 50, 73.0);
[carte1J1 setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#",imagecarte1] ] forState:UIControlStateNormal] ;
[carte1J1 addTarget:self action:#selector (clicCarte1J1) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:carte1J1 atIndex:1];
My view is viewJoueur1, I try to add my button in the view like this.
[viewJoueur1 addSubview:carte1J1];
And to test I try to hide viewJoueur1:
viewJoueur1.hidden=YES;
But the button is still visible and I don't understand why
In the first section of code that you listed, you have:
[self.view insertSubview:carte1J1 atIndex:1];
In the second section, you have:
[viewJoueur1 addSubview:carte1J1];
So, you've added cartelJ1 to two views if I'm understanding correctly.
Then, you hide one of those two views, but self.view is still visible, and it contains cartelJ1, so cartelJ1 is still visible.
Please correct me if I'm misunderstanding your code...
You’re right, that isn’t the documented behavior:
Hiding a view with subviews has the
effect of hiding those subviews and
any view descendants they might have.
You’re adding carte1J1 as a subview of viewJoueur1; is viewJoueur1 a plain UIView or a custom subclass? If it’s a subclass, have you overridden -setHidden:?
If it’s a standard UIView, then this behavior is not as documented and you should report it as a bug.
One thing that I notice is that when you create the button, you add it as a subview of self.view, then later add it as a subview of viewJoueur1. Views can only be a subview of one view at a time, so the first one is redundant.

rightBarButtonItem info button, no space to the right

I have a UIViewController set up to display an info-button on the right in its UINavigationItem like this:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(toggleAboutView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];
That works perfectly, but the only problem is that I'm left with little to no space to the right of the button, which looks pretty ugly:
Is there any way for the button to move about 5 px out from the right side?
Thanks.
infoButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
Try setting infoButton.contentEdgeInsets (or imageEdgeInsets); something like
infoButton.contentEdgeInsets = (UIEdgeInsets){.right=10};
I'm not sure if it works for "system" buttons, but it might.
If that doesn't work, the easiest way is probably to stick it inside a UIView.
EDIT: Presumably the toolbar calls [customView sizeThatFits:blah] to find out how big the view "wants to be". The above code is supposed to mean "add a 10-pixel right edge inset", which for a normal (custom) button also makes the size bigger by 10 pixels.
It seems like UIButton returns a fixed size for the standard info button, ignoring any insets. Your code "works" by affecting the contentRectForBounds: and imageRectForContentRect: methods, which changes where the icon is drawn, but does not actually change the size of the button. I suspect that the icon is drawing outside the button, which may mean that the "touchable" rect is wrong (but toolbars seem to increase the touch rects for normal UIBarButtonItems, so maybe it doesn't matter).