Make UIView and contents expand to fill available horizontal space in UIToolbar with Auto Layout

丶灬走出姿态 提交于 2019-12-13 19:39:26

问题


I have a UIBarButtonItem and a UIView containing two UITextField controls, all within a UIToolbar.

This is a screenshot of my storyboard: http://i.8px.co/x8eC.png.

I'd like to create something similar to this: http://i.8px.co/1hvY.png.

I'm trying to make the UIView and its children expand to fill the available horizontal space in the UIToolbar with Auto Layout. I don't know how to accomplish what I want.

I experimented with a dirty solution for the UIView and was able to have it size somewhat correctly.

view.width = toolBar.frame.width - button.width 
// where view is the UIView, toolBar is the UIToolBar, and button is the UIBarButtonItem

I don't believe this is very elegant. This is done programmatically, the UIView doesn't conform to the standard UIToolbar margins, and the solution doesn't apply to the UITextFields.


回答1:


I'm afraid you'd have to do this programmatically.

Try this :

  self.toolBarview.frame = (CGRect) {.origin = self.toolBarview.frame.origin.x, self.toolBarview.frame.origin.y, .size = [UIScreen mainScreen].bounds.size.width - self.barButton.width - 40, self.toolBarview.frame.size.height}; 

Here toolBarview is the UIView which has the 2 UITextField's. 40 is the buffer value that you need to subtract to give the padding.

/* EDIT */

You should give the appropriate constraints to the UITextField as shown below:




回答2:


"I'm trying to make the UIView and its children expand to fill the available horizontal space in the UIToolbar with Auto Layout" You can't. The UIView is in a UIBarButtonItem, and a UIBarButtonItem is not a view. So you can't use auto layout on it. You must set its width manually.




回答3:


You can do this in many way,

Do these Constraints

Then create a sub class of UITextField, and do this code

class CustomTextField: UITextField {

override func textRectForBounds(bounds: CGRect) -> CGRect {
    return CGRectInset(CGRectMake(65, 0, bounds.size.width-65, bounds.size.height) , 0 , 0 )
}

override func editingRectForBounds(bounds: CGRect) -> CGRect {
    return CGRectInset(CGRectMake(65, 0, bounds.size.width-65, bounds.size.height) , 0 , 0 )
}
}

In storyboard/xib, Connect textField to your CustomTextField

Create outlet for 2 textFields

@IBOutlet weak var textField1: CustomTextField!
@IBOutlet weak var textField2: CustomTextField!

in viewDidLoad() method,

  var label1 : UILabel = UILabel(frame: CGRectMake(5, (textField1.frame.height/2)-15, 100, 30))
    label1.textColor = UIColor.lightGrayColor();
    label1.text = "From:"

    var label2 : UILabel = UILabel(frame: CGRectMake(5, (textField1.frame.height/2)-15, 100, 30))
    label2.textColor = UIColor.lightGrayColor();
    label2.text = "To:"

    textField1.addSubview(label1)
    textField2.addSubview(label2)

Then you will get output like...



来源:https://stackoverflow.com/questions/32131432/make-uiview-and-contents-expand-to-fill-available-horizontal-space-in-uitoolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!