在文本视图中添加button

我有一个文本视图,其中我想要一个相机button。 在我的应用程序中,我希望用户键入一些文本,然后他/她想要附上照片或video。 我已经这样做了,但我想在文本视图内的相机button。 当我尝试添加一个button作为button滚动文本,但我想button固定和另一个问题是文本进来的button区域不可见,所以我希望光标不会进入该button覆盖的区域。

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; myButton.frame = CGRectMake(0, 0, 50.0, 30.0); [editButton addTarget:self action:@selector(editPressed:)forControlEvents:UIControlEventTouchUpInside]; myButton.backgroundColor=[UIColor blueColor]; [txtView addSubview:myButton]; 

在这里输入图像说明在这里输入图像说明

  • 试试这个。它一定会为你工作并解决你的问题

  • 在ViewDidLoad方法中写下面的代码。

 -(void)textResizeForButton{ CGRect buttonFrame = self.button.frame; UIBezierPath *exclusivePath = [UIBezierPath bezierPathWithRect:buttonFrame]; self.textView.textContainer.exclusionPaths = @[exclusivePath]; } 

将它添加到不在UITextField上的self.view ,以便它不会在任何地方滚动。 刚刚得到正确的CGrect

为了抵消右边的文字以适应UIButton试试这个:

  UIView *rightMarginView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, leftMargin, txtField.frame.size.height)]; rightMarginView.userInteractionEnabled = NO; [txtField setRightView:rightMarginView]; txtField.rightViewMode = UITextFieldViewModeAlways;