更改UIPopoverView背景+箭头颜色
有没有办法简单地更改iOS8上的UIPopoverView背景颜色(包括其箭头)?
(我读过一些关于定制“UIPopoverControllers”的文章,这里是否也适用,意思是“否”?
这是不是我应该能够在prepareForSegue方法触发popup? 我怎样才能达到相应的观点来改变它的外观?
我find了解决scheme。 iOS8不再需要子类化! 可以从tableview – > navigation – > popoverPresentationController中像这样访问和更改背景
self.navigationController?.popoverPresentationController?.backgroundColor = UIColor.redColor()
有关WWDC会议的更多信息214。
你可以简单地修改popover像这样:
let popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("popoverSegue") popoverViewController!.popoverPresentationController?.delegate = self popoverViewController!.modalPresentationStyle = .Popover let popoverSize = CGSize(width: 150, height: 60) popoverViewController!.preferredContentSize = popoverSize let popover = popoverViewController!.popoverPresentationController popover?.delegate = self popover?.permittedArrowDirections = .Up popover?.sourceView = self.view //change background color with arrow too! popover?.backgroundColor = UIColor.whiteColor() popover?.sourceRect = CGRect(x: self.view.frame.width, y: -10, width: 0, height: 0) presentViewController(popoverViewController!, animated: true, completion: nil)
更新:这个答案已经过时了。
苹果已经改进了在popup窗口中显示UIViewController的方式。 查看UIViewController
上的popoverPresentationController
属性。
- 将UIPopoverController上的popoverBackgroundViewClass设置为您自己的UIPopoverBackgroundView的子类:
- 子类UIPopoverBackgroundView。
-
实施方法:
+ (CGFloat)arrowHeight; // returns the height of the image you use for the arrow + (CGFloat)arrowBase; // returns the base of the image you use for the arrow + (UIEdgeInsets)contentViewInsets;
-
实现layoutSubviews,使箭头在正确的地方:
CGFloat xOffset = self.arrowImageView.frame.origin.x; CGFloat yOffset = self.arrowImageView.frame.origin.y; if (self.arrowDirection == UIPopoverArrowDirectionUp){ xOffset = (self.bounds.origin.x + self.bounds.size.width / 2.0f) + self.arrowOffset; xOffset -= [[self class] arrowBase]/2; } else if (self.arrowDirection == UIPopoverArrowDirectionDown){ xOffset = (self.bounds.origin.x + self.bounds.size.width / 2.0f) + self.arrowOffset; yOffset = self.bounds.origin.y + self.bounds.size.height - [[self class] arrowHeight; xOffset -= [[self class] arrowBase]/2; } else if (self.arrowDirection == UIPopoverArrowDirectionRight) { yOffset = floor(self.bounds.origin.y + ((self.bounds.size.height - [[self class] arrowBase]) / 2.0f) + self.arrowOffset); xOffset = (self.bounds.origin.x + self.bounds.size.width) - [[self class] arrowHeigtht]; } else if (self.arrowDirection == UIPopoverArrowDirectionLeft){ yOffset = floor(self.bounds.origin.y + ((self.bounds.size.height - [[self class] arrowBase]) / 2.0f) + self.arrowOffset); } CGRect frameForArrowImageView = CGRectMake(xOffset, yOffset, self.arrowImageView.frame.size.width, self.arrowImageView.frame.size.height); self.arrowImageView.frame = frameForArrowImageView; // this custom method applies a rotation on the imageView to rotate for the various arrow directions [self updateTransform]; // this custom method adjusts the borderViews frame to compensate for the changes due to the arrow rotation [self updateBorderViewFrame];