Изменение фона UIPopoverView + цвет стрелки
Есть ли способ просто изменить цвет фона UIPopoverView (включая его стрелку) на iOS8?
(Я прочитал несколько статей по настройке "UIPopoverControllers". Это также относится к этому вопросу, то есть ответ "нет"?)
![enter image description here]()
Разве это не то, что я должен учесть в методе prepareForSegue, запускающем popover? Как я могу достичь соответствующего представления, чтобы изменить его внешний вид?
Ответы
Ответ 1
Я нашел решение. Подкласс не нужен больше с iOS8! Фон может быть доступен и изменен таким образом изнутри tableview → navigation → popoverPresentationController
self.navigationController?.popoverPresentationController?.backgroundColor = UIColor.redColor()
Дополнительная информация об этом в сессии WWDC 214.
Ответ 2
Вы можете просто изменить 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)
Ответ 3
UPDATE: этот ответ устарел.
Apple улучшила способ отображения UIViewControllers в popover. Проверьте свойство popoverPresentationController
на UIViewController
.
- установить popoverBackgroundViewClass на UIPopoverController в ваш собственный подкласс 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];