Ios 爆米花不';t居中按钮
我正试着把一个爆米花放在一个按钮的中心。我似乎不知道我可能会错在哪里。而不是箭头在按钮的中间,它偏离了屏幕宽度的一半。Ios 爆米花不';t居中按钮,ios,swift,uipopovercontroller,Ios,Swift,Uipopovercontroller,我正试着把一个爆米花放在一个按钮的中心。我似乎不知道我可能会错在哪里。而不是箭头在按钮的中间,它偏离了屏幕宽度的一半。 @IBAction func buttonClicked(sender: AnyObject){ var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ServiceOptions") as! ServiceOptionsPopover pop
@IBAction func buttonClicked(sender: AnyObject){
var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ServiceOptions") as! ServiceOptionsPopover
popoverViewController.delegate = self
popoverViewController.modalPresentationStyle = .Popover
popoverViewController.preferredContentSize = CGSizeMake(300, 300)
let popoverPresentationViewController = popoverViewController.popoverPresentationController
popoverPresentationViewController?.permittedArrowDirections = .Up
popoverPresentationViewController?.delegate = self
popoverPresentationViewController?.sourceView = sender as! UIButton
popoverPresentationViewController?.sourceRect = sender.frame
presentViewController(popoverViewController, animated: true, completion: nil)
}
以下是正确的方法:
@IBAction func buttonClicked(sender: UIButton){
var popoverViewController = UIViewController()
popoverViewController.view.frame = CGRectMake(0,0, 300, 300)
popoverViewController.view.backgroundColor = UIColor.redColor()
popoverViewController.modalPresentationStyle = .Popover
popoverViewController.preferredContentSize = CGSizeMake(300, 300)
let popoverPresentationViewController = popoverViewController.popoverPresentationController
popoverPresentationViewController?.permittedArrowDirections = .Up
popoverPresentationViewController?.sourceView = sender
popoverPresentationViewController?.sourceRect = CGRectMake(0, 0, sender.bounds.width,sender.bounds.height) // see this line of code
presentViewController(popoverViewController, animated: true, completion: nil)
}
问题是混淆框架和边界的基本问题:
popoverPresentationViewController?.sourceView = sender as! UIButton
popoverPresentationViewController?.sourceRect = sender.frame
不!你的意思是:
popoverPresentationViewController?.sourceView = sender as! UIButton
popoverPresentationViewController?.sourceRect = (sender as! UIButton).bounds
原因是
sourceRect
是在sourceView
的坐标空间中给出的-也就是说,如果您希望它是视图的rect,它是该视图的边界。iOS 9中有一个问题。在故事板中设置锚定:
…导致箭头未位于锚点的中心:
要解决此问题,请将其添加到prepareforsgue:sender:
:
// Fixes popover anchor centering issue in iOS 9
if let popoverPresentationController = segue.destinationViewController.popoverPresentationController, sourceView = sender as? UIView {
popoverPresentationController.sourceRect = sourceView.bounds
}
在我的例子中,问题是不同的,弹出框显示的是带有自定义视图的UIBarButtonItem。对于iOS 11,如果使用UIBarButtonItem的自定义视图,则自定义视图需要是自动布局友好的 使用此类别,可以快速应用约束 UIView+NavigationBar.h
@interface UIView (NavigationBar)
- (void)applyNavigationBarConstraints:(CGFloat)width height:(CGFloat)height;
- (void)applyNavigationBarConstraintsWithCurrentSize;
@end
UIView+NavigationBar.m
#import "UIView+NavigationBar.h"
@implementation UIView (NavigationBar)
- (void)applyNavigationBarConstraints:(CGFloat)width height:(CGFloat)height
{
if (width == 0 || height == 0) {
return;
}
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:height];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:width];
[heightConstraint setActive:TRUE];
[widthConstraint setActive:TRUE];
}
- (void)applyNavigationBarConstraintsWithCurrentSize {
[self applyNavigationBarConstraints:self.bounds.size.width height:self.bounds.size.height];
}
@end
然后你可以做:
UIButton *buttonMenu = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonMenu setImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
buttonMenu.frame = CGRectMake(0, 0, 44, 44);
[buttonMenu addTarget:self action:@selector(showMenu:) forControlEvents:UIControlEventTouchUpInside];
//Apply constraints
[buttonMenu applyNavigationBarConstraintsWithCurrentSize];
UIBarButtonItem *menuBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonMenu];
应用约束时,popover将在自定义视图上正确显示,例如,将警报显示为popover的代码为:
UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"Menu" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
//Add actions ....
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.sourceView = buttonMenu;
popController.sourceRect = buttonMenu.bounds;
[self presentViewController:controller animated:YES completion:nil];
非常好的回答。截图帮助我确定这正是我遇到的问题,不是为我工作。我正在使用这个代码。让popoverViewController=segue.destinationViewController popoverViewController.modalPresentationStyle=UIModalPresentationStyle.Popover popoverViewController.popoverPresentationController!。如果让popoverPresentationController=segue.destinationViewController.popoverPresentationController,sourceView=sender as,则委派=自我?UIView{popoverPresentationController.sourceRect=sourceView.bounds}对我来说就像一个魅力!