ios - How to align 2 buttons to left & right border of a UIToolbar -
I am experimenting with Koko and Swift (I'm a Nob) and came into a problem which I can not seem to solve. Suppose that I will create a UIToolbar and add two buttons to it: 'Cancel' and 'Accept' I click the Cancel button on the left side of the UIToolbar (which works, of course, default Form) and align the right edge to the button. Is there an easy way to get the right alignment?
to toolbar = UIToolbar (frame: CGRectMake (0, 0, w, 35)) toolbar.backgroundColor = UIColor (red: 0.75, Green: 0.75 Blue This is what I currently : 0.75 Alpha: 0.95) to acceptButton = UIBarButtonItem (title :, "accept" style: UIBarButtonItemStylekDone, goal: self action: "ButtonPressed") to cancelButton = UIBarButtonItem (title: "Cancel", style: UIBarButtonItemStyle Goal Space = UIBarButtonItem (customView: UIView (frame: CGRectMake (0, 0, 50, 10)) // Simple, Target: Self, Action: "buttonPressed: - MEH! It's not dynamic! toolbar.items = [acceptButton, space, cancelButton] view.addSubview (toolbar) I'm a lot to answer using it to make a UIBarButtonItem place, but it will not be dynamic ( And so on suggest this solution - not good) so I expected that I would get the UIBarButtonItems frame but I can not do it either, they do not have any .frame property. So in another, it was suggested that:
UIView * view = (UIView *) [self.toolbar.subviews object import index: 0]; For the first item, this does not work either; It seems that there is no direct subview-child UIBarButtonItem (and therefore gives width as 375) . The installation tag is not a go-to ...:
go to cancelButton.tag = 1 go acceptButton.tag = 0 go acceptButtonFrame = toolbar.viewWithTag (0)! Go to frameBurntonFrame = toolbar.viewWithTag (1)! Frame println (acceptButtonFrame.width) yields println (cancelButtonFrame.width)
:
375.0
Fatal error: An Alternative Unexpectedly zero value while opening the value
(lldb)
Is there any way? : - / any help appreciated.
Yes, just add a flexible place between them
the toolbar = UIToolbar (frame: CGRectMake (0, 0 ,, W 35)) toolbar.backgroundColor = UIColor (red: 0.75, green: 0.75 blue: 0.75 Alpha: 0.95) to acceptButton = UIBarButtonItem (title: "accept", style: UIBarButtonItemStylekDone, goal: self action: "ButtonPressed") to cancelButton = UIBarButtonItem (title: "Cancel", style: UIBarButtonItemStylekPlain, goal: self action: "ButtonPressed") Go flexibleSpace = UIBarButtonItem (barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, Target: zero Or, action: zero); Toolbar.items = [acceptButton, flexibleSpace, cancelButton] Not sure that this is the correct syntax , but the class is still. The only difference is that SystemItem is a UIBarButtonSytemItem.FlexibleSpace
Comments:.
Then it was suggested to answer one more:
UIView * view = (UIView *) [self.toolbar.subviews object import index: 0]; For the first item, this does not work either; It seems that there is no direct subview-child UIBarButtonItem (and therefore gives width as 375) .is Objective-C code, not swift codeThe reason is that it does not work because
[0 self.toolbar. Subviews objectAtIndex]. >