在iOS Objective-C应用程序中,我试图在键盘顶部找到一个栏。我希望键盘通过位于此栏中的UITextView“激活”。但是我无法显示该栏。
我的理解:
- (void)viewDidLoad
{
myUIView=[[UIView alloc]initWithFrame:xxx]; // 1. Create an UIView that will serve as inputAccessoryView
myTextView=[[UITextView alloc]init]; // 2. Create a UItextView
[myUIView addSubview:myTextView]; // 3. Add it to the UIView
[myTextfield setInputAccessoryView:myUIView]; // 4. Set the UIview as being the inputAccessoryView of the textView
// [self.view addSubview:myUIView]; // If I do this, the view is displayed but the app crashes when tapping in the textField
}
然而,视图没有显示,textView也没有显示 我试图将textView作为主控制器视图的子视图,当textview显示时,myUIView不会
我在这种方法中做错了什么?
答案 0 :(得分:1)
您没有正确使用InputAccessoryView
。您要将UITextView
添加到InputAccessoryView
作为子视图,其中UITextView
应添加到view
(如UIViewController
的视图中实例)
您需要像InputAccessoryView
一样将UITextView
分配给InputAccessoryView
,就是这样,当键盘显示时,您的- (void)viewDidLoad {
[super viewDidLoad];
UITextView *textView = [UITextView new];
textView.frame = CGRectMake(100, 100, 100, 40);
textView.backgroundColor = [UIColor redColor];
UIView *accessoryView = [UIView new];
accessoryView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44);
accessoryView.backgroundColor = [UIColor greenColor];
textView.inputAccessoryView = accessoryView;
[self.view addSubview:textView];
}
视图将位于键盘区域上方。
请查看下面的示例,了解如何在代码中添加它:
override func viewDidLoad() {
super.viewDidLoad()
let textView = UITextView()
textView.frame = CGRect(x: 100, y: 100, width: 100, height: 40)
textView.backgroundColor = .red
let accessoryView = UIView()
accessoryView.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 44)
accessoryView.backgroundColor = .green
textView.inputAccessoryView = accessoryView
view.addSubview(textField)
}
或Swift版
public class Book
{
[Key]
public int BookId { get; set; }
public string Name { get; set; }
public string AuthorName { get; set; }
public int YearOfPublishing { get; set; }
public LibraryType Type { get; set; }
public ICollection<BookPublicHouse> PublicHouses { get; set; }
public Book()
{
PublicHouses = new Collection<BookPublicHouse>();
}
}
public class PublicHouse
{
[Key]
public int PublicHouseId { get; set; }
public string PublicHouseName { get; set; }
public string Country { get; set; }
public ICollection<BookPublicHouse> Books { get; set; }
public PublicHouse()
{
Books = new Collection<BookPublicHouse>();
}
}
public class BookPublicHouse
{
public Book Book { get; set; }
public int BookId { get; set; }
public PublicHouse PublicHouse { get; set; }
public int PublicHouseId { get; set; }
}
以下是具有绿色背景的输入附件视图的快照
答案 1 :(得分:0)
提供的不同解决方案有效,但要求我的textView位于inputAccessoryView之外。
我的解决方案基于page的可用工作,完全符合我的期望。
简而言之,它的作用是覆盖inputAccessoryView函数,以提供动态构建的UIView,其中包含textView。