my buttons and textview not display on screen in iOS? - ios

i am creating three buttons and one uitextview dynamically but when i run the program its not display on my screen
this my code to create dynamic buttons and textview
-(void)getSmsdisplay
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]
applicationFrame]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextView *textview=[[UITextView alloc]init];
[button setTitle:#"Comment" forState:UIControlStateNormal];
[button1 setTitle:#"Share" forState:UIControlStateNormal];
[button1 setTitle:#"Like" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self
action:#selector(btncomment:)forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self
action:#selector(btnShare:)forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(Like:)forControlEvents:UIControlEventTouchUpInside];
smsdata *data=[[smsdata alloc]init];
[textview setText:data.Data];
[self.view addSubview:textview];
//add the button to the view
[self.view addSubview:button];
[self.view addSubview:button1];
[self.view addSubview:button2];
}

your coding is fine , but u r not added the frame for UIButton and UItextview
-(void)getSmsdisplay
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]
applicationFrame]];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextView *textview=[[UITextView alloc]init];
button.frame=CGRectMake(20, 50, 200, 50);
button1.frame=CGRectMake(20, 150, 200, 50);
button2.frame=CGRectMake(20, 300, 200, 50);
button.backgroundColor=[UIColor redColor];
button1.backgroundColor=[UIColor grayColor];
button2.backgroundColor=[UIColor blackColor];
[button setTitle:#"Comment" forState:UIControlStateNormal];
[button1 setTitle:#"Share" forState:UIControlStateNormal];
[button2 setTitle:#"Like" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self
action:#selector(btncomment:)forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self
action:#selector(btnShare:)forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(Like:)forControlEvents:UIControlEventTouchUpInside];
[textview setText:#"karthik"];
[self.view addSubview:textview];
//add the button to the view
[self.view addSubview:button];
[self.view addSubview:button1];
[self.view addSubview:button2];
}
the output is

You are assigning self.view to another UIView instance, which has not been added to the viewController. Note that self.view is already initialised in a viewController and it is dangerous to reassign the same.
Also, you have not set any frames for the Buttons and textview.
The following code should make it work.
-(void)getSmsdisplay
{
UIView *smsView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]
applicationFrame]];
[self.view addSubview: smsView]; //The new view needs to be added to something!
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UITextView *textview=[[UITextView alloc]initWithFrame:CGRectMake:(20,20,100,50)];
[button setTitle:#"Comment" forState:UIControlStateNormal];
[button1 setTitle:#"Share" forState:UIControlStateNormal];
[button1 setTitle:#"Like" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self
action:#selector(btncomment:)forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self
action:#selector(btnShare:)forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self
action:#selector(Like:)forControlEvents:UIControlEventTouchUpInside];
smsdata *data=[[smsdata alloc]init];
[textview setText:data.Data];
[smsView addSubview:textview];
button.frame=CGRectMake(20, 50, 200, 50);
button1.frame=CGRectMake(20, 150, 200, 50);
button2.frame=CGRectMake(20, 300, 200, 50);
//add the button to the view
[smsView addSubview:button];
[smsView addSubview:button1];
[smsView addSubview:button2];
}

Related

UINavigationController centered button using obj-c

I'm trying to add a centered button in my navbar header, confused as to how I can do it
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:#selector(twitterButton)];
[self.titleView //i read this can be used to like set it but i havent been able to figure it out
help would be appreciated, pretty new
check this code
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
container.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setBackgroundImage:[UIImage imageNamed:#"button0.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = container;
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender {
NSLog(#"button clicked");
}
I think its pretty straight, you can use titleView property of UINavigationItem :
-(void)addCenterButton {
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)];
[button setImage:meImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(twitterButton) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
}

uitextfield is shown but the button is not shown

UITextField is shown but not the UIButton.
How can I show the button ??
UITextField *password = [[UITextField alloc] initWithFrame:CGRectMake(30.0, 150, 300, 30.0)];
password.delegate = self;
[password setBorderStyle:UITextBorderStyleRoundedRect];
[password setClearButtonMode:UITextFieldViewModeAlways];
password.placeholder = #"Password";
[password setFont:[UIFont systemFontOfSize:10]];
[self.view addSubview:password];
//button
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 200 , 50, 50)] ;
[button addTarget:self action:#selector(clicked) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button = [UIButton buttonWithType: UIButtonTypeSystem];
[self.view addSubview:button];
Try to add BackgroundColor and TitleColor of button:
[button setBackgroundColor:[UIColor greenColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
you are intializing button two times.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 200 , 50, 50)] ;
button = [UIButton buttonWithType: UIButtonTypeSystem];
Intialize system button and set its frame later.
UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
button.frame = CGRectMake(30, 200 , 50, 50);
Hope this will help.

Why is UIButton blurry when creating programatically

i created a uibutton like so:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(280, 320, 100, 30);
[button setTitle:#"World" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:button];
and the button's text looks edgy, someone know why?
Change
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
to
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
^^^^^^
All will be good..
Try giving Font :
// Configure the cell...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 100, 30);
[button setTitle:#"World" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.font=[UIFont fontWithName:#"HelveticaNeu-CondensedBold" size:16];
button.backgroundColor= [UIColor clearColor];
[cell.contentView addSubview:button];

Adding two button via coding in viewDidLoad section is always hiding one button

Adding two button via coding in viewDidLoad section is always hiding one button, and I don't know why this is happening.
Here is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
//array initialization
list=[[NSMutableArray alloc]init];
heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
heading.text=#"Lists";
heading.backgroundColor=[UIColor clearColor];
[self.view addSubview:heading];
UIImage *addButton=[UIImage imageNamed:#"add.png"];
add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
[add addTarget:self action:#selector(addList) forControlEvents:UIControlEventTouchUpInside];
[add setImage:addButton forState:UIControlStateNormal];
[self.view addSubview:add];
UIImage *contact=[UIImage imageNamed:#"contacts.png"];
UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
add.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
[add addTarget:self action:#selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
[add setImage:contact forState:UIControlStateNormal];
[self.view addSubview:cntctBtn];
}
This looks like a copy and paste error. You never set the frame, image, or target action for the second button because you have "add" where you should have "cntctBtn".
- (void)viewDidLoad {
[super viewDidLoad];
//array initialization
list=[[NSMutableArray alloc]init];
heading=[[UILabel alloc]initWithFrame:CGRectMake(140.0f, 5.0f, 40.0f, 20.0f)];
heading.text=#"Lists";
heading.backgroundColor=[UIColor clearColor];
[self.view addSubview:heading];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
addButton.frame = CGRectMake(190.0f, 6.0f, 20.0f, 20.0f);
[addButton addTarget:self action:#selector(addList) forControlEvents:UIControlEventTouchUpInside];
[addButton setImage:[UIImage imageNamed:#"add.png"] forState:UIControlStateNormal];
[self.view addSubview: addButton];
UIImage *contact=[UIImage imageNamed:#"contacts.png"];
UIButton *cntctBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
cntctBtn.frame = CGRectMake(100.0f, 6.0f, 20.0f, 20.0f);
[cntctBtn addTarget:self action:#selector(shareContact) forControlEvents:UIControlEventTouchUpInside];
[cntctBtn setImage:contact forState:UIControlStateNormal];
[self.view addSubview:cntctBtn];
}
Its a copy paste error. Above is the corrected code.

xcode invisible button with text

I know I could do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
but that leaves a border around the button.
I could also do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then put a UILabel behind the button.
Is there a way to accomplish this ^ without needing two objects?
UPDATE:
To clarify my question:
I want the have text, only text, that when pressed performs a method call. For reasons that are complicated to explain I need to do this by way of a button. So, how do I make the text of a button the only visible part of the button. No border. No background. Just text.
UIButton *btnLikeUserCount = [[[UIButton alloc] init] autorelease];
[btnLikeUserCount.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
btnLikeUserCount.frame = CGRectMake(posx,posy,width,height);
[btnLikeUserCount setTitle:text forState:UIControlStateNormal];
[btnLikeUserCount setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
[btnLikeUserCount addTarget:self action:#selector(btnLikeUserCountClick:) forControlEvents:UIControlEventTouchUpInside];
no need to take label object.
I can't get what you want exactly but you might be missed [self.view addSubview:button] in your code. so you can not display button with text.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Your second code is working fine. In the below image hello is a button.
My code is:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(110, 100, 100, 50);
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
in this way u can do this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 400, 100, 50);
[button setTitle:#"Button text" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button.layer setBorderColor: [UIColor clearColor]];
[button.layer setBorderWidth: 0.0];
[self.view addSubview:button];

Resources