InputAccessoryView 边框?
Posted
技术标签:
【中文标题】InputAccessoryView 边框?【英文标题】:InputAccessoryView Border? 【发布时间】:2013-05-11 18:56:17 【问题描述】:我正在创建自定义键盘附件视图。我目前已经完成了大约 95% 的工作。我唯一剩下的就是删除原生 ios 键盘上方的 1px(或 2px?)行。有没有办法为 inputaccessoryview 添加边框,以便我可以使用与键盘渐变相同的颜色来消除黑条?
Picture Example
如果寄宿生是不可能的,那么对我的源代码做的下一个最合乎逻辑的事情是什么?
signInViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import <AudioToolbox/AudioToolbox.h>
@interface signInViewController : UIViewController <UITextFieldDelegate, UITextViewDelegate>
AVAudioPlayer *sound;
UIButton *myButton;
UITextField *textFieldOne;
UIButton *gmailButton;
UIButton *meButton;
UIButton *yahooButton;
UIButton *outlookButton;
UIView *inputAccView;
UIButton *aolButton;
UIButton *milButton;
- (IBAction)playSwoosh:(id)sender;
- (IBAction)dismiss:(id)sender;
- (IBAction)toggleUIButtonImage:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *textFieldOne;
@property (weak, nonatomic) IBOutlet UITextField *textFieldTwo;
@property (nonatomic, retain) UIButton *gmailButton;
@property (nonatomic, retain) UIButton *meButton;
@property (nonatomic, retain) UIButton *yahooButton;
@property (nonatomic, retain) UIButton *outlookButton;
@property (nonatomic, retain) UIButton *aolButton;
@property (nonatomic, retain) UIButton *milButton;
@property (retain, nonatomic) UIButton *myButton;
@property (nonatomic, retain) UIView *inputAccView;
@end
和signInViewController.m
#import "signInViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface signInViewController ()
@end
@implementation signInViewController
@synthesize myButton = _myButton;
@synthesize textFieldOne = _textFieldOne;
@synthesize textFieldTwo = _textFieldTwo;
@synthesize inputAccView;
@synthesize gmailButton;
@synthesize yahooButton;
@synthesize meButton;
@synthesize outlookButton;
@synthesize aolButton;
@synthesize milButton;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
return self;
- (void)gmailButtonTapped
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:@"@gmail.com"];
- (void)meButtonTapped
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:@"@me.com"];
- (void)outlookButtonTapped
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:@"@outlook.com"];
- (void)yahooButtonTapped
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:@"@yahoo.com"];
- (void)aolButtonTapped
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:@"@aol.com"];
- (void)milButtonTapped
self.textFieldOne.text = [self.textFieldOne.text stringByAppendingString:@"@mail.mil"];
- (void)createInputAccessoryView
inputAccView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 310.0, 40.0)];
[inputAccView setBackgroundColor:[UIColor colorWithRed:0.569 green:0.600 blue:0.643 alpha:1.000]];
[inputAccView setAlpha: 0.8];
// GMAIL BUTTON ADDED
gmailButton = [UIButton buttonWithType: UIButtonTypeCustom];
[gmailButton setFrame:CGRectMake(9.5, 4.5, 60.0, 30.0)];
[gmailButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:gmailButton];
gmailButton.layer.cornerRadius = 5;
gmailButton.layer.masksToBounds = NO;
gmailButton.layer.shadowColor = [UIColor blackColor].CGColor;
gmailButton.layer.shadowOpacity = 0.8;
gmailButton.layer.shadowRadius = 2;
gmailButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[gmailButton setTitle:@"@Gmail" forState:UIControlStateNormal];
[gmailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[gmailButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
gmailButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue Bold" size:12];
[gmailButton addTarget:self action:@selector(gmailButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// ME BUTTON ADDED
meButton = [UIButton buttonWithType: UIButtonTypeCustom];
[meButton setFrame:CGRectMake(73.5, 4.5, 40.0, 30.0)];
[meButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:meButton];
meButton.layer.cornerRadius = 5;
meButton.layer.masksToBounds = NO;
meButton.layer.shadowColor = [UIColor blackColor].CGColor;
meButton.layer.shadowOpacity = 0.8;
meButton.layer.shadowRadius = 2;
meButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[meButton setTitle:@"@Me" forState:UIControlStateNormal];
[meButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[meButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
meButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue Bold" size:12];
[meButton addTarget:self action:@selector(meButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// YAHOO BUTTON ADDED
yahooButton = [UIButton buttonWithType: UIButtonTypeCustom];
[yahooButton setFrame:CGRectMake(117.5, 4.5, 65.0, 30.0)];
[yahooButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:yahooButton];
yahooButton.layer.cornerRadius = 5;
yahooButton.layer.masksToBounds = NO;
yahooButton.layer.shadowColor = [UIColor blackColor].CGColor;
yahooButton.layer.shadowOpacity = 0.8;
yahooButton.layer.shadowRadius = 2;
yahooButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[yahooButton setTitle:@"@Yahoo" forState:UIControlStateNormal];
[yahooButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[yahooButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
yahooButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue Bold" size:12];
[yahooButton addTarget:self action:@selector(yahooButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// OUTLOOK BUTTON ADDED
outlookButton = [UIButton buttonWithType: UIButtonTypeCustom];
[outlookButton setFrame:CGRectMake(186.5, 4.5, 77.0, 30.0)];
[outlookButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:outlookButton];
outlookButton.layer.cornerRadius = 5;
outlookButton.layer.masksToBounds = NO;
outlookButton.layer.shadowColor = [UIColor blackColor].CGColor;
outlookButton.layer.shadowOpacity = 0.8;
outlookButton.layer.shadowRadius = 2;
outlookButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[outlookButton setTitle:@"@Outlook" forState:UIControlStateNormal];
[outlookButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[outlookButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
outlookButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue Bold" size:12];
[outlookButton addTarget:self action:@selector(outlookButtonTapped) forControlEvents:UIControlEventTouchUpInside];
// AOL BUTTON ADDED
aolButton = [UIButton buttonWithType: UIButtonTypeCustom];
[aolButton setFrame:CGRectMake(267.5, 4.5, 41.0, 30.0)];
[aolButton setBackgroundColor:[UIColor colorWithRed:(241/255.0) green:(242/255.0) blue:(242/255.0) alpha:1]];
[inputAccView addSubview:aolButton];
aolButton.layer.cornerRadius = 5;
aolButton.layer.masksToBounds = NO;
aolButton.layer.shadowColor = [UIColor blackColor].CGColor;
aolButton.layer.shadowOpacity = 0.8;
aolButton.layer.shadowRadius = 2;
aolButton.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
[aolButton setTitle:@"@Aol" forState:UIControlStateNormal];
[aolButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[aolButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
aolButton.titleLabel.font = [UIFont fontWithName:@"Helvetica Neue Bold" size:12];
[aolButton addTarget:self action:@selector(aolButtonTapped) forControlEvents:UIControlEventTouchUpInside];
- (IBAction)backgroundTouched:(id)sender
[_textFieldOne resignFirstResponder];
[_textFieldTwo resignFirstResponder];
- (void)textFieldDidBeginEditing:(UITextField *)textField
if (textField == self.textFieldOne)
[self createInputAccessoryView];
[textField setInputAccessoryView:inputAccView];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 95), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
else if (textField == self.textFieldTwo)
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 95), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
- (void)textFieldDidEndEditing:(UITextField *)textField
if (textField == self.textFieldOne)
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 95), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
else if (textField == self.textFieldTwo)
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 95), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
- (BOOL)textFieldShouldReturn:(UITextField *)textField
NSInteger nextTag = textField.tag + 1;
UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
if (nextResponder)
[nextResponder becomeFirstResponder];
else
[textField resignFirstResponder];
return NO;
【问题讨论】:
也。我知道有一个 inputAccView.layer.border = 1;但是,这并没有做accview 的外部,它只与内部接壤。我已经试过了。 【参考方案1】:好吧,在玩弄了它之后,我决定不添加寄宿生,因为它所做的只是包括了一个内部寄宿生。我只是简单地画了一个物体来掩盖它。但我确实为那些仍然想知道如何做的人找到了一个很好的资源!这是另一种非常适合实施的方法。对于那些只想快速修复的新开发人员,有人提议让我简单地创建另一个按钮,该按钮与屏幕的长度相同,高度为 3 px 以覆盖它,但我们都知道这是错误的答案。只是给任何新手的提示
Extend iOS Keyboard Gradient
【讨论】:
【参考方案2】:根据https://github.com/ardaogulcan/react-native-keyboard-accessory 的文档,KeyboardAccessoryView
有一个特殊属性 hideBorder
我想InputAccessoryView
具有相同的属性。
【讨论】:
以上是关于InputAccessoryView 边框?的主要内容,如果未能解决你的问题,请参考以下文章
仅在启用 darkTheme 时将边框颜色应用于 TextField
没有为 UITextField 正确添加 inputAccessoryView