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.
Related
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;
}
Like image shows, I used UIBarbuttonItem as rightBarButtonItem, what I want to do is just change rightBarButton's title when I select a picture or deselect a picture, so I used code like this:
[self.rightBarButton setTitle:[NSString stringWithFormat:#"完成(%#/%#)",#(_selectedAssets.count),#(self.maxPicturesNum)]];
but the barbutton will show shin when its text changed, I try to use UIButton instead of UIBarButtonItem, and shin effect disappear indeed, but UIButton will be close to right bounds far, can you help me?
You can create UIButton and assign it to your rightBarButton
UIButton *rightButton = [[UIButton alloc] initWithFrame:someFrame];
[rightButton setBackgroundImage:someImage forState:UIControlStateNormal];
[rightButton addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
rightButton.titleLabel.text = [NSString stringWithFormat:#"完成(%#/%#)",#(_selectedAssets.count),#(self.maxPicturesNum)];
rightButton.titleLabel.font = someFont;
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.rightBarButton = rightBarButtonItem;
And while updating label use rightButton.titleLabel.text
For Adjusting label use this:
- (void)setTitlePositionAdjustment:(UIOffset)adjustment
forBarMetrics:(UIBarMetrics)barMetrics
Finally,I do like this:
- (void)loadRightButtonItem{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(30, 0, 120, 40);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
[button setTitle:#"完成(0/9)" forState:UIControlStateNormal];
button.tintColor = [UIColor whiteColor];
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
// [button setTitleColor:[self.buttonFolder titleColorForState:UIControlStateHighlighted] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(onFinishClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 135, 40)];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:view];
[view addSubview:button];
self.navigationItem.rightBarButtonItem = item;
_rightBarButton = button;
}
It cancel the shin effect.
thanks to #RoHaN,your idea did great inspired me.
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];
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];
}
I tried to set Rounded button with the following but its disappearing from the view. if I removed the
takebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
then its displaying the rectangular button. help me to get the RoundedRect Button.
takebtn = [[UIButton alloc]initWithFrame:CGRectMake(30, 325, 250, 40)];
takebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takebtn setTitle:#"Take Photo" forState:UIControlStateNormal];
takebtn.backgroundColor=[UIColor colorWithRed:50.0/255.0 green:205.0/255.0 blue:50.0/255.0 alpha:1.0];
[takebtn addTarget:self
action:#selector(takePhoto:)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[nextview addSubview:takebtn];
You need to modify your code like that below:-
takeBtn.layer.cornerRadius = 10; //value can be change accordingly.
takeBtn.clipsToBounds = YES;
Note:- No need to import Quartz framework in ios7
try this
UIButton* takebtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[takebtn setFrame:CGRectMake(30, 325, 250, 40)];
[takebtn setTitle:#"Take Photo" forState:UIControlStateNormal];
takebtn.backgroundColor=[UIColor colorWithRed:50.0/255.0 green:205.0/255.0 blue:50.0/255.0 alpha:1.0];
[takebtn addTarget:self
action:#selector(takePhoto:)
forControlEvents:(UIControlEvents)UIControlEventTouchDown];
[nextview addSubview:takebtn];