iPhone UITextField and UIButton in one grey frame - iphone

I would like to create something like this:
I would like to place an UITextField with an UIButton into one grey frame, and I would like the button's border to be contained by this grey frame.
Please somebody show me some sample code for this!
Thx!

1. Use a UIImageView of required rect. Set the background image to the gradient gray colour image.
UIImageView*myImageView=[[UIImageView alloc] initWithFrame:yourFrame];
[myImageView setImage:[UIImage imageNamed:#"grayBackground.png"];
[self.view addSubview:myImageView];
2. Use a round rect. UITextField make it a subview of the image view. Use a place holder as "Write a reply..." make it a subview to your imageview.
UITextField*myField=[[UITextField alloc] initWithFrame:yourRect];
[myField setBorderStyle:UITextBorderStyleRoundedRect];
[myField setPlaceholder:#"Write a reply..."];
[myImageView addSubview:myField];
3. Use a UIButton with type Custom and the send image as its background image, make it a subview to your imageview.
UIButton*myButton=[UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:yourRect]
[myButton setBackgroundImage:[UIImage imageNamed:#"sendImage.png"] forState:UIControlStateNormal];
[myImageView addSubView:myButton];

Simple way is to create title bar with grey background and delete the title.and place textfield and button which u want to place it.configure the size and button outside the title bar and drag it to the title bar..u'll get like the image which u have got.

Related

Set UIImageView background color upon user intervention

I am developing an application in which I have a horizontal scroller.
The scroller area comprise of different colors [which are nothing but UIImageView of colors].
Above the scroller area is a canvas [which is also a UIImageView].
Now, what I want is that when I select any color in the scroller area, it should set on the canvas.
It is pretty much like a color picker but I am already providing solid color options to be selected by the user.
How should I go about it ? Any sample Code to get me started or my mind kicking would be awesome ?
Thanx a ton
To implement it easily.
Use UIView not UIImageView
Set UIView backgroundColor and place it on the scroll
Set UITapGestureRecognizer on views,Refer here
when a purticular view is touched you have its instance of view which
is touched
get the background color of the view and set it in the scroller
Can you take UIButtons instead of UIImageViews inside your scrollView to show colours to be picked.
This will make the implementation easier. User will select any colour button and you will receive a callback in buttons action selector method. Then based on button tag or any property you can set the colour to your canvas.
In current implementation it will be tricky as you need to know the exact content offset where the tap is done to make out which UIImageView was pressed.
coding steps:
In your scroll view add UIButtons instead of UIImageView like:
UIButton* button = [[UIButton alloc] initWithFrame:someRect];
//put some tag to button
button.tag = someInt;
//add selector to each button to get callback
[view addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
In the btnSelected method put this code:
-(IBAction)btnSelected:(id)sender;
{
UIButton* button = (UIButton*) sender;
if (button.tag == someInt) {
//do something like
canvas.backgroundColor = button.backgroundColor;
}
}
Instead of using UIImageViews or UIViews as suggested, I would put custom UIButtons for picking colors.
UIButton * colorButton;
//You will probably do these inside a for loop
colorButton = [UIButton buttonWithType:UIButtonTypeCustom];
[colorButton setFrame:CGRectMake(X, Y, buttonSize, buttonSize)];
[colorButton setTitle:#"" forState:UIControlStateNormal];
[colorButton setBackgroundColor:SOME_COLOR];
//Border visualization would be good if you are putting buttons side-by-side
[colorButton.layer setBorderWidth:1.0];
[colorButton.layer setBorderColor:[[UIColor darkGrayColor] CGColor]];
//You can use this tag to identify different buttons later
[colorButton setTag:INDEX+SOME_OFFSET]; //use loop index with some offset
[colorButton addTarget:self action:#selector(colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
-(void) colorButtonPressed:(UIButton *) sender
{
UIColor *selectedColor = sender.backgroundColor; //Or identify with sender.tag
[yourCanvas doSmtOnCanvas:selectedColor];
}
Initialize UIImageView then add gesture recognizer to all imageViews by calling this method.
- (void) setupImageView : (UIImageView *) imageView{
[imageView setUserInteractionEnabled:YES];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(changeUIImageViewBackgroundColor:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tapGestureRecognizer];
}
Now change color of canvasView within the selector by following way:
- (void) changeUIImageViewBackgroundColor : (UITapGestureRecognizer *) recognizer{
UIImageView *imageView = (UIImageView *)[recognizer view];
[canvasView setBackgroundColor:[imageView backgroundColor]];
}
You can put a tap gesture in your scrollview and whenever a tap is made in the scroll check whether the tap is on the imageview. (You will get whether the tap point in on imageview or not) and according you can change the image.
Second alternative is to take a custom button and handle on its click.
Hope it helps

Gray Overlay on a custom UIButton getting displayed

I have created a custom UIButton for an iOS 4.3+ application. I wanted to make the background image stretch, so I and used the following methods to set its background image:
[myButton setBackgroundImage:[[UIImage imageNamed:myImagePath] stretchableImageWithLeftCapWidth:leftStretchValue topCapHeight:topStretchValue] forState:UIControlStateNormal]
[myButton setBackgroundImage:[[UIImage imageNamed:myHighlightedImagePath] stretchableImageWithLeftCapWidth:eftStretchValue topCapHeight:topStretchValue] forState:UIControlStateHighlighted]
[myButton setBackgroundImage:[[UIImage imageNamed:mySelectedImagePath]
stretchableImageWithLeftCapWidth:eftStretchValue topCapHeight:topStretchValue] forState:UIControlStateSelected]
I have added an event handler to the custom button
[myButton addTarget:self action:#selector(buttonHighlighted:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:#selector(buttonSelected:) forControlEvents:UIControlEventTouchUpInside];
In the above mentioned methods, I have set the selected and the highlighted state of the button accordingly. I have changed the frame of the UIButton to adjust the dimensions of an irregular image (mySelectedImage and myHighlightedImage, neither of which is a rectangle), so that it gets aligned properly along with myImage.
- (void) buttonSelected {
myButton.selected = !myButton.selected; //setting the selected property to NO in the viewDidLoad
//code to change the frame of the UIButton to accomodate the irregular image
}
- (void) buttonHighlighted {
myButton.highlighted = YES;
}
The problem I am facing is that when I select myButton, a gray color overlay is displayed on myImage for a very short time before the images myHighlightedImage and mySelectedImage are displayed as its background. I have set the background color of myButton as clearColor, thinking that might be the reason, but it is not getting solved.
What could be the possible issue... ?
button.adjustsImageWhenHighlighted = NO;
I think that is what you are looking for..

iPhone: How to make UIButton which you can easily tap?

I'm making customized navigation bar by using UIView and UIButtons instead of UINavigationBar.
But my UIButtons on the navigation bar doesn't response sensitively.
I have to tap almost center of the UIButton to tap.
It doesn't respond if I tap edge of the UIButton.
But buttons on normal UINavigationBar can be tapped by tapping edge of the button.
Even by tapping outside of the button, it can be tapped.
Shutter button or Option button on the camera app also can be tapped by tapping edge or outside of buttons.
How can I implement those easily tappable buttons to my app?
Use an image and create a custom button. Set the button so the image does not scale to the size of the button's view, but instead will just Center. Expand the button's size so it is larger than the image on each side. Apple does this as well with things like tab buttons.
UIButton has an imageEdgeInsets property specially for this purpose. Just make a UIButton frame as big as you need for touchable area and than scale image inside it appropriately, using imageEdgeInsets.
Disclaimer: This code has not been tested, but it gives you an idea of how it could be done.
You make a button (in this case 40px x 40px), and then add a background image to it which is smaller, hence gives the impression of that the image is very "clickable".
// This image is 20px x 20px (Just an example)
UIImage* backgroundImage = UIImage imageNamed:#"backgroundImage.png"]
// Custom button, remember to add a target method
UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
customButton.contentMode = UIViewContentModeCenter;
[customButton setImage:backgroundImage forState:UIControlStateNormal];
UIBarButtonItem* customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.rightBarButtonItem = customBarButtonItem;
[customBarButtonItem release];

adding image in UILabel in iPhone

i have UIlabel with image.my text are won't come into top of the image i want to show my text in top of the image.
image1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"salarytax.png"]];
this is my code to adding image to label. this image is a output of that code. how can i show the text in top of the image
try adding the UILabel on the image
[myimageview addSubView: myLabel];
[myImageView bringSubviewToFront: myLabel];
If you have just taken the UILabel and trying to set its background as an image then dont think it would create a problem.
If you are able to view the text on the label but you want the text to be vertically on top then you can set its contentVerticalAlignment property to top.
Hope this helps..
UILabel *imageLabel;
[imageLabel setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"logo.png"]]];
[imageLabel setText:#"Khalid Usman"];
[imageLabel setTextColor:[UIColor blackColor]];
Note: I have checked it for conformation then i post. It's working.

how to overlay an image in iphone application?

I want to overlay an image over a button. the button has a background image. If i want to overlay a semi transparent image over the button, suppose i overlay a red color image, which is semi transparent. Can any one suggest how to do that? Suggest some documentation.
UIImageView *overlay = [[UIImageView alloc] initWithImage:...];
[overlay setAlpha:0.5];
UIButton *button = [[UIButton alloc] initWithType:UIButtonTypeRoundedRect];
[button setBackgroundImage:...];
[button addSubview:overlay];
[overlay release];
UIButton also has an image property as well as a backgroundImage property, although I'm unsure how transparency would work with it.
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html
Following dc answer, note that changing the button alpha will affect also the overlay transparency, so check that button alpha is 1.0f (default value).
Note also that you should release the overlay variable after adding it as subview to button, otherwise, you'll get a memory leak.