Par*_*iya 29 landscape objective-c uiimagepickercontroller ios uiimageorientation
我正在创建一个横向模式的应用程序,我正在UIImagePickerController使用iPhone相机拍摄照片,我也希望以横向模式创建它.
但是,正如Apple文档所暗示的UIImagePickerController那样,不支持横向定位,那么我该如何才能获得所需的功能呢?
Mc.*_*ver 47
如果您想在横向模式下使用UIImagePickerController,请使用user1673099的答案,而不是:
- (BOOL)shouldAutorotate
{
return NO;
}
Run Code Online (Sandbox Code Playgroud)
使用:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
Run Code Online (Sandbox Code Playgroud)
然后选择器将以横向模式打开:

但请确保在部署信息中检查Portrait:

Dav*_*vid 32
......我想在横向模式下创建它.
一行代码可以产生很大的不同!在您的IBAction登陆的方法或功能中:
在斯威夫特,
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
// .overCurrentContext allows for landscape and portrait mode
imagePickerController.modalPresentationStyle = .overCurrentContext
Run Code Online (Sandbox Code Playgroud)
Objective-C中,
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
[imagePickerController setDelegate:self];
[imagePickerController setModalPresentationStyle: UIModalPresentationOverCurrentContext];
Run Code Online (Sandbox Code Playgroud)
注意:这将允许imagePickerController提出它的观点正确,但将 可同时呈现不解决旋转的问题.
use*_*099 14
试试这个......
根据Apple Document,ImagePicker Controller从不在横向模式下旋转.您只能在纵向模式下使用.
仅对于ImagePicker控制器禁用横向模式,请遵循以下代码:
在您的ViewController.m中:
制作Image Picker Controller的SubClass(NonRotatingUIImagePickerController)
@interface NonRotatingUIImagePickerController : UIImagePickerController
@end
@implementation NonRotatingUIImagePickerController
// Disable Landscape mode.
- (BOOL)shouldAutorotate
{
return NO;
}
@end
Run Code Online (Sandbox Code Playgroud)
使用方法如下
UIImagePickerController* picker = [[NonRotatingUIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
etc.... Just as Default ImagePicker Controller
Run Code Online (Sandbox Code Playgroud)
这对我有用,如果您有任何问题,请告诉我.
这适用于iOS 10/11中的Swift 4.0.
import UIKit
extension UIImagePickerController {
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .all
}
}
Run Code Online (Sandbox Code Playgroud)
只需将扩展名放在项目的某个位置,就不需要为了它的工作而继承子类.
如果确实需要指定设备类型,可以添加如下检查:
import UIKit
extension UIImagePickerController {
override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
}
}
Run Code Online (Sandbox Code Playgroud)
这将允许iPad自由旋转,但在手机上强制执行纵向模式.只需确保您的应用程序配置为在其info.plist中支持这些,否则您可能会在启动选择器时遇到崩溃.
这是一个支持所有界面方向旋转的版本:
/// Not fully supported by Apple, but works as of iOS 11.
class RotatableUIImagePickerController: UIImagePickerController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .all
}
}
Run Code Online (Sandbox Code Playgroud)
这样,如果用户旋转她的设备,它将更新选择器控制器以支持当前方向.像往常一样实例化UIImagePickerController.
如果您只想支持方向子集,则可以返回不同的值.
UIImagePickerController在没有任何黑客攻击的横向模式中使用的正确方法是将其放入UIPopoverController
- (void)showPicker:(id)sender
{
UIButton *button = (UIButton *)sender;
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_popover = [[UIPopoverController alloc] initWithContentViewController:picker];
[_popover presentPopoverFromRect:button.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28966 次 |
| 最近记录: |