如何在iPhone上扩展键盘渐变?

6
我发现很少有应用程序扩展键盘,但我想知道他们是如何做到的。以下是两个例子:TextasticPrompt
我知道可以将inputAccessoryView添加到UITextView中,但仍然存在一条细小的线,它将键盘与UIToolbar分开,就像下面的图片一样。他们是如何做到的?扩展持有键盘的UIWindow还是以其他方式?
更新1:我已经使用了Tyraz编写的解决方案:
1. 子类化UIToolbar 2. 我使用了UIView而不是图片,该UIView的背景颜色与键盘渐变的完成颜色相同,并使用UIViewAutoResizingMaskFlexibleWidth,以便在旋转时覆盖键盘,高度为3像素
以下是子类化UIToolbar的代码:
- (void)didMoveToSuperview {

    [self.separatorHideView removeFromSuperview];

    CGRect seperatorRect = CGRectMake(self.frame.origin.x,
                                      self.frame.size.height,
                                      self.frame.size.width,
                                      3.0);

    self.separatorHideView = [[UIView alloc]];
    self.separatorHideView.backgroundColor = [UIColor colorWithRed:0.569 green:0.600 blue:0.643 alpha:1.000];
    self.separatorHideView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [self addSubview:self.separatorHideView];
}

更新2: 这是我将其添加到UITextView中的代码,以及我用作色调的颜色。
我在viewDidLoad中使用以下代码将其添加到UITextView中。
    CustomToolbar *accessoryToolbar = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 38)];
    accessoryToolbar.tintColor = [UIColor colorWithRed:0.569 green:0.600 blue:0.643 alpha:1.000];
    editor.inputAccessoryView = accessoryToolbar;  

这是应用解决方案后的效果

enter image description here


嗨,阿玛尔!我正在寻求改进我的输入附件视图(即UIToolbar),使其具有与此处描述的相同外观。您介意发布分隔符隐藏视图背景颜色的确切颜色设置吗?以及您如何对UIToolbar进行着色,以使其具有与键盘匹配的渐变效果?非常感谢您提前的帮助! - Nenad M
嗨,Nenad。没问题。我不知道为什么上面的代码中没有显示颜色,但我会修复它。 - Amar Kulo
2个回答

8

在 iOS 7 上,你可以轻松地创建一个 inputAccessoryView 来与键盘样式相匹配:

[[UIInputView alloc] initWithFrame:<#frame#> 
                    inputViewStyle:UIInputViewStyleKeyboard];

4

我建议使用inputAccessoryView加上第二个视图来覆盖分隔线并“填补空白部分”。

一旦将inputAccessoryView添加到键盘中(通过在accessory UIView子类中重写didMoveToSuperview方法以在发生此情况时得到通知),请将其添加到inputAccessoryView的superview中。

在accessory UIView子类中应该是这样的:

- (void)didMoveToSuperview {
  [self.separatorHideView removeFromSuperview];

  CGRect seperatorRect = CGRectMake(self.frame.origin.x, 
                                    self.frame.origin.y + self.frame.size.height,
                                    self.frame.size.width,
                                    2.0);

  UIImage *gapGradient = [UIImage imageNamed:@"GapGradient"];
  self.separatorHideView = [[UIImageView alloc]initWithImage:gapGradient];
  self.separatorHideView.frame = seperatorRect;

  [self.superview addSubview:self.separatorHideView];
}

我建议你在你的辅助UIView子类中重写setFrame方法,以便在键盘框架发生变化时更新gapView的框架。


这是问题的解决方案。我将用答案更新问题。 - Amar Kulo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接