How to create a very custom uibutton like UITableViewCellStyleSubtitle in iOS - ios

I would like to create a button with an image, title and description similar to the UITableViewCellStyleSubtitle. I would like to do this programmatically.
Does anyone know how I can accomplish this?

You could create a category of the UIButton class. Here's a barebones setup for you:
In the .h file:
#interface UIButton (YourCategoryName)
- (void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description;
#end
In the .m file:
#implementation UIButton (YourCategoryName)
- void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description {
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 50)]];
[self addSubview:titleLabel];
[titleLabel release];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image];
[self addSubview:imageView];
[imageView release];
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 200, 100)];
[self addSubview:descriptionLabel];
[descriptionLabel release];
}
In the above code, you can adjust the frames as needed. Once this has been setup, just create your button like normal in your view/viewcontroller and call the method above:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0, 0, 300, 200);
[myButton setupButton:myTitle image:myImage description:myDesc];

Subclass UIControl and override drawRect and/or add subviews to create the desired appearance.

Related

Button creates at tableFooterView not response to events

I create a tableView in a class inherit from UIView.It works well when I add a button at tableFooterView on this tableView.
CustomView.h
#import <UIKit/UIKit.h>
#interface CustomView : UIView<UITableViewDelegate,UITableViewDataSource>
#end
CustomView.m
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
UIButton * tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
tempButton.center = customView.center;
tempButton.backgroundColor = [UIColor redColor];
[tempButton addTarget:self action:#selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:tempButton];
return customView;
}
- (void)doButtonAction{
NSLog(#"123");
}
NSLog:123
However,I public this button and make a target-action in my controller.It response nothing.
CustomView.h
#property (strong, nonatomic) UIButton * tempButton;
CustomView.m
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView * customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 80)];
self.tempButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
self.tempButton.center = customView.center;
[customView addSubview:self.tempButton];
return customView;
}
ViewController.h
- (void)viewDidLoad {
[super viewDidLoad];
CustomView *cv = [[CustomView alloc] initWithFrame:self.view.bounds ];
[cv.tempButton addTarget:self action:#selector(doButtonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:cv];
}
- (void)doButtonAction{
NSLog(#"123");
}
what did I miss?
In the viewdidload you are not set any thing on the button,
You just assign in the #interface.
but you must be alloc and set the frame and backgroundcolor and all properties on the button in the viewdidload .
After that it will work.
May be it will help you or feel free.

UIButton not changing anything with #selector - new Issue

Need help. I am trying to change an image of an ImageView using a programmed button. I copied some of the sample codes out here. The sitting on highlighted button works but not doing anything else when clicked. Thanks.
Thanks for the replies. I managed to change the photos but created more problem. I believe it is clear that that I am new to Xcode.
New problems:
1. The new image stays on top so i cannot access the button.
2. I would like to have the image changed only when the button is highlighted.
3. With the new image, it created warning: "Hides instance variable" on the relayImages properties. Thanks again.
.h file
-(void)changeLabel:(UIButton *)sender;
-(void)addMyButton;
-(void)addMyImages;
-(void)addMyLabel;
#property (nonatomic) UIButton *playButton;
#property (nonatomic) UIImageView *relayImages;
#property (nonatomic, strong) UILabel *checkStatusLabel;
#synthesize relayImages;
.m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self addMyImages];
[self addMyButton];
[self addMyLabel];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)addMyButton
{
UIButton *playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 348, 20, 78)];
UIImage *offState = [UIImage imageNamed:#"PBNoOff.png"];
[playButton setBackgroundImage:offState forState:UIControlStateNormal];
UIImage *onState = [UIImage imageNamed:#"PBNoOn.png"];
[playButton setBackgroundImage:onState forState:UIControlStateHighlighted];
[playButton addTarget:self action:#selector(changeLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
}
-(void)changeLabel:(UIButton *)sender
{
UIImage *image1 = [UIImage imageNamed:#"1_2RelaysOn.png"];
UIImageView *relayImages = [[UIImageView alloc] initWithImage:image1];
relayImages.frame = CGRectMake(0, 0, 320, 480);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
[self.view addSubview:_playButton];
}
-(void)addMyImages
{
UIImage *image = [UIImage imageNamed:#"1_2RelaysOff.png"];
UIImageView *relayImages = [[UIImageView alloc] initWithImage:image];
relayImages.frame = CGRectMake(0, 0, 320, 480);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
}
-(void)addMyLabel
{
UILabel *checkStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 200, 30)];
checkStatusLabel.backgroundColor = [UIColor greenColor];
checkStatusLabel.textColor = [UIColor whiteColor];
[self.view addSubview:checkStatusLabel];
}
try this i am created custom imageView
.m file
-(void)addMyButton
{
_playButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 348, 60, 50)];
[_playButton setTitle:#"button" forState:UIControlStateNormal];
[_playButton setBackgroundColor:[UIColor grayColor]];
UIImage *offState = [UIImage imageNamed:#"1.jpg"];
// [playButton setBackgroundImage:offState forState:UIControlStateNormal];
// UIImage *onState = [UIImage imageNamed:#"PBNoOn.png"];
[_playButton setBackgroundImage:offState forState:UIControlStateHighlighted];
[_playButton addTarget:self action:#selector(changeLabel:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_playButton];
}
-(void)changeLabel:(UIButton *)sender
{
UIImage *image1 = [UIImage imageNamed:#"2.jpg"];
relayImages = [[UIImageView alloc] initWithImage:image1];
relayImages.frame = CGRectMake(20, 100, 100, 100);
relayImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:relayImages];
}
-(void)addMyImages
{
UIImage *image = [UIImage imageNamed:#"3.jpg"];
UIImageView *someImages = [[UIImageView alloc] initWithImage:image];
someImages.frame = CGRectMake(20, 200, 100, 100);
someImages.backgroundColor = [UIColor whiteColor];
[self.view addSubview:someImages];
}
-(void)addMyLabel
{
_checkStatusLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 250, 100, 30)];
_checkStatusLabel.backgroundColor = [UIColor greenColor];
_checkStatusLabel.textColor = [UIColor whiteColor];
[self.view addSubview:_checkStatusLabel];
}
.h file
-(void)changeLabel:(UIButton *)sender;
-(void)addMyButton;
-(void)addMyImages;
-(void)addMyLabel;
#property (nonatomic) UIButton *playButton;
#property (nonatomic) UIImageView *relayImages;
#property (nonatomic, strong) UILabel *checkStatusLabel;
and viewdidload
[self addMyButton];
[self addMyImages];
[self addMyLabel];

How to set the default image to UIButton in Viewdidload in Objective-C?

I try to set Image for a UIButton. But how to set the default image for UIButton ?
The code of UIButton in header file is like the following.
#property (strong, nonatomic) IBOutlet UIButton *modeChangeButton;
And I try to set the image size and default image for UIButton , but it doesn't show the image. The code is like the following.
- (void)viewDidLoad
{
self.modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
}
how to set the default image for UIButton ?
Thanks in advance.
If you are using IBOutlet, you no need to reallocate your button. Please try as below
Using IBOutlet :
- (void)viewDidLoad
{
self.modeChangeButton.frame = CGRectMake(200, 450, 60, 60);
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
}
Using Programmatically:
- (void)viewDidLoad
{
self.modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
[self.view addSubView:self.modeChangeButton];
}
Also, Check your Y position of your button. Whether it is out of view frame.
You forgot to add your button as a subview.
[self.view addSubview:self.modeChangeButton];
- (void)viewDidLoad
{
[super viewDidLoad];
self.modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"]
forState:UIControlStateNormal];
}
You have missed calling viewDidLoad of super. I am guess that might be the issue.

rightView property error in iOS7 for UITextField

I am setting an imageView as the rightView of a textfield. This is my code:
UITextField *textField = [[UITextField alloc]initWithFrame:controlFrame];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[textField setBackgroundColor:[UIColor whiteColor]];
CGRect imageViewFrame = CGRectMake(controlFrame.origin.x + controlFrame.size.width - 20,controlFrame.origin.y, 15.0,controlFrame.size.height-10);
UIImage *image = [UIImage imageNamed:#"arrow.png"];;
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setFrame:imageViewFrame];
[textField setRightViewMode:UITextFieldViewModeAlways];
textField.rightView = imageView;
Its working perfect on iOS 6.1 devices
But in iOS 7 devices
Any Solution for this?
Thanks in advance
Was just working on this myself and used this solution:
- (CGRect) rightViewRectForBounds:(CGRect)bounds {
CGRect textRect = [super rightViewRectForBounds:bounds];
textRect.origin.x -= 10;
return textRect;
}
This will move the image over from the right by 10 instead of having the image squeezed up against the edge in iOS 7.
Additionally, this was in a subclass of UITextField, which can be created by:
Create a new file that's a subclass of UITextField instead of the default NSObject
Add a new method named - (id)initWithCoder:(NSCoder*)coder to set the image
- (id)initWithCoder:(NSCoder*)coder {
self = [super initWithCoder:coder];
if (self) {
self.clipsToBounds = YES;
[self setRightViewMode:UITextFieldViewModeUnlessEditing];
self.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"textfield_edit_icon.png"]];
}
return self;
}
You may have to import #import
Add the rightViewRectForBounds method above
In Interface Builder, click on the TextField you would like to subclass and change the class attribute to the name of this new subclass
There's another faster option, no need to subclass your uitextfield. There's an example
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 6)];
[imageView setImage:[UIImage imageNamed:#"grey_filter.png"]];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 6)];
[paddingView addSubview:imageView];
Just create a View bigger than your image with your padding. Add your UIImageView and then put it as left/right view in your textfield.

Button on cameraOverlayView not working

I have the following cameraOverlayView that is working to an extent. When the UIImagePickerController is called the standard controls are not shown and the 'Take Photo' button and logo.jpg are shown as described in ObViewControllerCameraOverlay. The button functions in that it goes blue when pressed but my problem is that it does not call the action (the NSLog is simply for test purposes). Can anyone see what I might have done wrong?
#implementation ObViewControllerCameraOverlay
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame])
{
UIImage *logo = [UIImage imageNamed:#"logo.jpg"];
UIImageView *logoView = [[UIImageView alloc] initWithImage:logo];
logoView.frame = CGRectMake(0, 0, 240, 50);
[self addSubview:logoView];
UIButton *takePhotoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takePhotoButton setTitle:#"Take photo" forState:UIControlStateNormal];
takePhotoButton.frame = CGRectMake(0, 330, 320, 40);
[self addSubview:takePhotoButton];
}
return self;
}
- (IBAction)takePhotoButton:(id)sender
{
NSLog(#"test");
}
It doesn't look like you've told your button to call takePhotoButton: when it's tapped. Try adding
[takePhotoButton addTarget:self action:#selector(takePhotoButton:) forControlEvents:UIControlEventTouchUpInside];
after you've created the button in initWithFrame:

Resources