我正在尝试将imageview的帧设置为图像的原始大小,并将其显示在视图的中心.我为此编写了以下代码,但图像仍然出现在整个屏幕上!假设图像大小为320x88,它显示调整大小/拉伸到320x460!我希望它出现在屏幕的中心,所以我将Y坐标设置为(460-88)/ 2,但它不起作用.有谁可以帮我解决这个问题.谢谢.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
if(imageView.image.size.width == IPHONE4_RES_WIDTH)
[imageView setFrame: CGRectMake(0, 0, imageView.image.size.width/2, imageView.image.size.height/2)];
UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
viewController.view.backgroundColor = [UIColor blackColor];
NSLog(@"%f : %f",viewController.view.bounds.size.height, imageView.bounds.size.height);
viewController.view = imageView;
viewController.view.contentMode = UIViewContentModeCenter;
[self.navigationController pushViewController:viewController animated:YES];
Run Code Online (Sandbox Code Playgroud) 请帮帮我.我很抱歉,如果它是重复但我没有得到必要的答案,这就是为什么我再次问.
我想在第一个视图中重新加载另一个视图的表.
谢谢,谢谢.
我正在尝试找到一种方法来捕获从segue到根控制器的返回,这样我就可以运行自定义代码来决定下一步该做什么.我已经尝试了下面的两种方法,他们没有做到这一点,因为他们似乎(显然)关心展开segue堆栈.有什么想法吗?
-(UIStoryboardSegue *)segueForUnwindingToViewController
{
}
-(BOOL)canPerformUnwindSegueAction
{
}
Run Code Online (Sandbox Code Playgroud) 当我的主视图旋转时,我想重新排列子视图,所以在我的ViewController中,我重写
willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration
Run Code Online (Sandbox Code Playgroud)
并在那里设置子视图的帧.这一切都很好,但在子视图中我也被覆盖了
layoutSubviews
Run Code Online (Sandbox Code Playgroud)
所以他们正确地布置自己.但问题是这个现在被调用两次 - 大概一次我在willAnimateRotationToInterfaceOrientation中设置Frame而一次因为旋转而设置.(如果我没有设置Frame,它会被调用一次.)
当然,ViewController负责布局框架,这看起来像是一个设计缺陷 - 什么是解决方案,所以layoutSubviews只调用一次?
我正试图在我的游戏上创建一个暂停屏幕.我在故事板中添加了一个'PauseScreen'viewController,故事板ID和Restoration ID设置为"PauseScreenID",并移动到暂停屏幕我已在"GameScene"中创建了该功能:
func pauseSceenTransition(){
let viewController = UIStoryboard(name: "Main", bundle:nil).instantiateViewControllerWithIdentifier("PauseScreenID") as UIViewController
let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate)
currentViewController.window?.rootViewController?.presentViewController(viewController, animated: false, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
但是当它被调用时,我得到错误:
警告:尝试在< AppName .StartScreenViewController:0x7fae61f79980>上显示< AppName .PauseScreen:0x7fae61fe5ff0>,其视图不在窗口层次结构中!
"StartScreenViewController"是我的开始屏幕的视图控制器,是初始视图控制器.然后转到"GameScene",这是"PauseScreen"需要去的地方.如果我将初始视图控制器设置为"GameViewController"/"GameScene",它可以工作,所以我认为我需要更改第二行:
let currentViewController = (UIApplication.sharedApplication().delegate as AppDelegate)
Run Code Online (Sandbox Code Playgroud)
所以它在"GameViewController"上呈现"PauseScreen",而不是"StartScreenViewController",但我不知道该怎么做.
我正在与iBeacons一起在AppDelegate.swift中实现CoreLocation方法(在AppDelegate中实现方法以确保App后台功能)
在“ ViewController.swift”随附的SingleView应用程序中,将数据从AppDelegate传递到ViewController以更新视图(例如UIImages,UILabels或UITableViews)的最佳实践是什么?
我已经成功实现了两种不同的方法:
1)委托,通过在AppDelegate.swift中触发Viewcontroller委托方法:
protocol BeaconLocationDelegate { func minorBeaconChanged(minorValue:NSNumber) }
var locationDelegate: BeaconLocationDelegate
locationDelegate?.minorBeaconChanged(nearestBeacon.minor)
Run Code Online (Sandbox Code Playgroud)
ViewController.swift:
在viewDidLoad方法中:
(UIApplication.sharedApplication().delegate as AppDelegate).locationDelegate = self
Run Code Online (Sandbox Code Playgroud)
(我发现这看起来很难看-将此属性声明为委托的更好方法吗?)
协议实现:
func minorBeaconChanged(minorValue: NSNumber) {
// do fancy stuff here
}
Run Code Online (Sandbox Code Playgroud)
2)通过在AppDelegate中创建对ViewController的引用:
let viewController:ViewController = window!.rootViewController as ViewController
viewController.doSomethingFancy()
Run Code Online (Sandbox Code Playgroud)
两种方法对我来说都很好用,但是我认为通过委派的第一种方法更优雅,并且在拥有多个ViewController时是更好的选择。
有什么建议吗?
我想实现一个单独的ErrorHandler类,它显示某些事件的错误消息.应该从不同的其他类调用此类的行为.发生错误时,它将具有UIAlertView输出.此AlertView的显示应始终位于顶部.因此无论从何处抛出Error,最顶层的viewController都应该显示AlertMessage(例如,当异步后台进程失败时,我想要一条错误消息,无论前景中显示什么View).
我找到了几个似乎可以解决我的问题的要点(见下面的代码).但是调用UIApplication.sharedApplication().keyWindow?.visibleViewController()确实会返回一个零值.
从要点扩展
extension UIWindow {
func visibleViewController() -> UIViewController? {
if let rootViewController: UIViewController = self.rootViewController {
return UIWindow.getVisibleViewControllerFrom(rootViewController)
}
return nil
}
class func getVisibleViewControllerFrom(vc:UIViewController) -> UIViewController {
if vc.isKindOfClass(UINavigationController.self) {
let navigationController = vc as! UINavigationController
return UIWindow.getVisibleViewControllerFrom( navigationController.visibleViewController)
} else if vc.isKindOfClass(UITabBarController.self) {
let tabBarController = vc as! UITabBarController
return UIWindow.getVisibleViewControllerFrom(tabBarController.selectedViewController!)
} else {
if let presentedViewController = vc.presentedViewController {
return UIWindow.getVisibleViewControllerFrom(presentedViewController.presentedViewController!)
} else {
return vc;
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我是Swift的新手,我在viewController中将方向锁定为肖像时遇到问题.实际上我已经在自定义导航控制器中使用此代码锁定了它
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if (self.visibleViewController is ViewController)
{
return UIInterfaceOrientationMask.Portrait
}
return .All
}
Run Code Online (Sandbox Code Playgroud)
一切正常,ViewController被锁定为肖像.问题是在横向模式下从另一个控制器返回到此控制器.如果我在横向上返回ViewController(从NextViewController返回),那么ViewController将出现在横向中.有什么建议吗?
我正在尝试制作一个具有多个视图但只有一个视图控制器的注册表单。进入下一个视图后,我将输入写入到一个结构中,该结构稍后将发送到服务器。我面临的问题是,进入新视图时,VC将重新初始化,因此用户结构也将重新初始化。有没有办法解决多个ViewControllers的问题?
我真的很挣扎这个基本的iOS编程的东西,但我无法弄清楚发生了什么以及如何解决它.
我有我的主Login控制器,用于检测用户何时登录,如果auth成功,则显示下一个控制器:
@interface LoginViewController (){
//Main root instance
RootViewController *mainPlatformRootControler;
}
-(void)loggedInActionWithToken:(NSString *)token anonymous:(BOOL)isAnon{
NSLog(@"User loged in.");
mainPlatformRootControler = [self.storyboard instantiateViewControllerWithIdentifier:@"rootViewCOntrollerStoryIdentifier"];
[self presentViewController:mainPlatformRootControler animated:YES completion:^{
}];
}
Run Code Online (Sandbox Code Playgroud)
这很好,没问题.
我的麻烦是处理注销.如何完全删除RootViewController实例并显示一个新实例?
我可以看到RootViewController实例正在堆叠,因为我有多个观察者,并且在注销后然后登录它们被多次调用(多次我退出并重新进入).
我试过以下但没有成功:
首先检测RootViewController中的注销并解除:
[self dismissViewControllerAnimated:YES completion:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"shouldLogOut" object:nil];
}];
Run Code Online (Sandbox Code Playgroud)
然后在LoginViewController中:
-(void)shouldLogOut:(NSNotification *) not{
NSLog(@"No user signed in");
mainPlatformRootControler = NULL;
mainPlatformRootControler = nil;
}
Run Code Online (Sandbox Code Playgroud)
那我怎么办呢?我知道它是一个基本的内存处理器,但我只是不知道如何?
viewcontroller ×10
ios ×5
swift ×5
iphone ×3
objective-c ×2
appdelegate ×1
delegation ×1
locking ×1
orientation ×1
rotation ×1
segue ×1
uialertview ×1
uiimageview ×1
uitableview ×1
uiwindow ×1
xcode6 ×1