[英]In UIImagePickerController Cancel button not showing?
這里我使用下面的代碼。 如果有人知道這個問題,請幫助我。 我也試過下面的網址,但它不起作用。 請幫我
iOS7 UIImagePickerController 取消按鈕消失UIPopoverController 內的 UIImagePickerController 在 iOS7 上不顯示取消按鈕
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
picker.navigationBar.tintColor = [UIColor redColor];
picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
picker.navigationBar.topItem.rightBarButtonItem.tintColor = [UIColor blackColor];
我的問題:
我的要求:
這對我有用:
present(imagePicker, animated: true, completion: {
imagePicker.navigationBar.topItem?.rightBarButtonItem?.tintColor = .black
})
看起來蘋果在它(iOS 10,Xcode 8)上犯了一些錯誤,因為無法完成更改 UIImagePickerController 的色調顏色,因為在控制器沒有topItem
屬性或navigationController
屬性之前。 所以已經完成了UIImagePickerController extension
的更改。 但是我在這些重寫的方法中檢查了navigationController
和topItem
: viewDidLoad
、 viewWillAppear
、 viewDidAppear
。 但它仍然nil
。 所以我決定在viewWillLayoutSubviews
檢查它,瞧! 它不是零,所以我們可以在這里設置精確 rightBarButtomItem 的條形着色顏色!
這是示例:
extension UIImagePickerController {
open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.black
self.navigationBar.topItem?.rightBarButtonItem?.isEnabled = true
}
}
並且不要忘記調用super.viewWillLayoutSubviews
,這非常重要;-) 編輯:但是返回相冊屏幕時仍然有問題..
如果一切努力都不起作用,那么您必須使用 appearance() 方法。 因此,嘗試在您想要呈現的任何視圖控制器(UIImagePickerController)中編寫以下行。 您可以在任何類中使用外觀,但您可能已將 UIBarButtonItem.appearance() 用於其他目的,因此請找到並在 viewDidDisappear() 中編寫該代碼。
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.red], for: .highlighted)
我認為您還沒有將 viewController 嵌入到導航控制器中
請這樣做並嘗試代碼:(目標 - c)
-(void)viewDidLoad
{
[super viewDidLoad];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
}];
UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 300)];
imageview.image=image;
}
我使用了您發布的相同代碼。 它顯示“取消”按鈕。
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
picker.navigationBar.tintColor = [UIColor redColor];
picker.navigationBar.barStyle = UIBarStyleBlackOpaque;
picker.navigationBar.topItem.rightBarButtonItem.tintColor = [UIColor blackColor];
[self presentViewController:picker animated:YES completion:NULL];
可能是您正在使用任何自定義導航欄,並且您將應用程序委托的屬性設置為更改導航 nar 的色調顏色。
似乎更新一些項目屬性使按鈕可見,所以......只需像這樣啟用它:
present(imagePicker, animated: true, completion: {
self.imagePicker.navigationBar.topItem?.rightBarButtonItem?.isEnabled = true
})
如果以上任何一項都不適合您,請嘗試在定義UIImagePickerController
時添加它
imagePicker.modalPresentationStyle = .popover // Or.fullScreen (Can also check cases for iPad and iPhone as per requirement)
像魅力一樣為我工作。 被這件事搞得一團糟
Swift 4.2 解決方案,將以下代碼添加到 AppDelegate 的 didFinishLaunchingWithOptions 中
UINavigationBar.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).tintColor = .black
子類 UIPickerViewController 如下:
class CustomImagePicker: UIImagePickerController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UINavigationBar.appearance().tintColor = UIColor.black
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.init(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)], for: .normal)
}
override func viewWillDisappear(_ animated: Bool) {
UINavigationBar.appearance().tintColor = UIColor.white // your color
UIBarButtonItem.appearance().setTitleTextAttributes(nil, for: .normal)
super.viewWillDisappear(animated)
}
}
並使用作為:
func openGallary()
{
picker!.sourceType = UIImagePickerController.SourceType.photoLibrary
picker!.modalPresentationStyle = .currentContext
self.present(picker!, animated: true, completion: nil)
}
更改 UIImagePickerController navigationBar.tintColor 試試這個代碼:
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor blueColor]];
UIImagePickerController *imgPicker=[[UIImagePickerController alloc]init];
imgPicker.delegate = self;
imgPicker.navigationBar.barStyle = UIBarStyleBlackOpaque;
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.allowsEditing = NO;
if([UIImagePickerController isSourceTypeAvailable: sourceType])
{
imgPicker.sourceType=sourceType;
[self presentViewController:imgPicker animated:YES completion:NULL];
}
這對我有用:
let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: .blue), NSFontAttributeName: UIFont.boldSystemFont(ofSize: 16.0)], for: .normal)
我遇到過這種同樣的問題,在浪費了很多時間之后我找到了解決方案。 我已經自定義了導航欄的外觀,就像下面的代碼一樣,為什么會發生這個問題。
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "ic_left")
let attributes = [NSAttributedString.Key.font: UIFont(name: "FuturaPT-Medium", size: 16)!]
UINavigationBar.appearance().titleTextAttributes = attributes
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "line.png"),for: .bottom,barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage(named: "line.png")
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
這只是導航欄的自定義外觀,我們只需要更改最后一行代碼,因為我已將標題顏色設置為清除。 這真的很簡單。 在展示您的圖像選擇器控制器之前放置此代碼。
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
再次,如果您不希望導航欄按鈕標題清除文本顏色。 謝謝
請在 swift 3 或更高版本中嘗試此代碼
let picker = UIImagePickerController()
picker.navigationBar.topItem?.rightBarButtonItem?.tintColor = UIColor.white
斯威夫特 5:
imagePicker.modalPresentationStyle = .fullScreen
試試這個代碼:
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:nil];
並添加委托方法: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:^{
}];
self.img = image;
[self.iconBtn setBackgroundImage:image forState:UIControlStateNormal];
}
我認為這會對您有所幫助。只需將代碼粘貼到 imagePickerControllerDidCancel 中,然后將其關閉(Swift 4)。
picker.setNavigationBarHidden(false, animated: true)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.