我的单例访问器方法通常是以下的一些变体:
static MyClass *gInstance = NULL;
+ (MyClass *)instance
{
@synchronized(self)
{
if (gInstance == NULL)
gInstance = [[self alloc] init];
}
return(gInstance);
}
Run Code Online (Sandbox Code Playgroud)
我可以做些什么来改善这个?
在Swift中这个函数的正确语法是什么?
以下工作正常,并将背景颜色为紫色:
self.view.backgroundColor = UIColor.purpleColor()
Run Code Online (Sandbox Code Playgroud)
当我链接colorWithAlphaComponent函数时,视图会显示正确的alpha值,然后更改为相对较暗的不透明紫色:
self.view.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0.5)
Run Code Online (Sandbox Code Playgroud)
这是向UIColor添加alpha值的推荐功能吗?
此外,为什么intellisense popup说这个函数需要UIColor作为参数?例如,
self.view.backgroundColor = UIColor.colorWithAlphaComponent(<#UIColor#>)
Run Code Online (Sandbox Code Playgroud)
编辑:行为很奇怪.我正在一个正在加载模式的视图控制器上设置背景颜色.当模态从底部向上滑动时,alpha是正确的.当模态完成加载时,背景颜色变为不透明?!
编辑2:问题不在于代码 - 上面的代码和下面的建议都正确地应用了alpha.问题在于呈现模态的方式 - 基础视图正在被删除.请参阅:
导航控制器上的透明模态视图
(Xcode6,iOS8,Swift,iPad)
我正在尝试创建一个类似于Web的经典模态视图,其中对话框的外部是"灰显的".为了实现这一点,我将模态的视图的backgroundColor的alpha值设置为0.5,如下所示:
self.view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,当模态变为全屏时,将删除呈现视图.(导航控制器上的参考透明模态视图).
(这里的概念有点恼火.为什么删除基础视图?根据定义,模态出现在其他内容之上.一旦基础视图被删除,它就不再是模态了.它介于模态和推动之间过渡.瓦娃......反正..)
为了防止这种情况发生,我在父控制器的方法中设置了modalPresentationStyleto ,并在Storyboard中......但没有运气.CurrentContextviewDidLoad
self.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.navigationController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
Run Code Online (Sandbox Code Playgroud)
当模态变为全屏时,如何防止显示视图被删除?
tyvm ..更多信息如下.
也在故事板中,如此(演示文稿:当前上下文)

感谢您的帮助...以下文档:

您之前可能已经看过它,它在像ScoutMob这样的消费时尚应用中变得非常流行.我正在尝试在启动时实现60%透明视图,这将覆盖我的主屏幕,解释如何使用主屏幕的功能并在点击时消失.
我完成了我的整个应用程序(它是从几年前开始使用.xib的,但是也可以用故事板格式来解释,因为我可能会在其他iOs 5.0+应用程序中重用此功能.)
我制作单个视图没有问题,但是暂时将一个视图叠加在另一个上面是我没有直观地想出来的.我将继续研究并包括我发现的任何有用的提示,以防其他人试图做同样的事情.
我有以下代码在iOS7中完美运行.
[UIView animateWithDuration:0.5 animations:^(void) {
self.view.alpha = 0.5;
[self.navigationController.navigationBar setAlpha:0.3];
}]; //to make the background view controller semi-transparent
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
[rootViewController setModalPresentationStyle:UIModalPresentationCurrentContext];
OverlayViewController *ctlr = [storyBoard instantiateViewControllerWithIdentifier:@"OverlayViewController"];
//present the OverlayViewController
[self presentViewController:ctlr animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
然后我在后台视图控制器的viewWillAppear中有以下内容,将其视图恢复为完全不透明.
[UIView animateWithDuration:1.0 animations:^(void) {
self.view.alpha = 1.0;
[self.navigationController.view setAlpha:1.0];
}];
Run Code Online (Sandbox Code Playgroud)
现在,使用iOS8,上面的代码不会将背景设置为半透明.OverlayViewController环绕黑色.
我在网上发现使用UIModalPresentationOverCurrentContext会给出预期的行为.它实际上确实如此,但背景视图控制器永远不会脱离视图层次结构(编辑以添加对此行为的引用:https://developer.apple.com/documentation/uikit/uimodalpresentationstyle).因此,从不调用viewWillAppear,因此永远不会删除半透明度.
显然,我可以使用像使用NSNotificationCenter的黑客,并在删除OverlayViewController时触发通知,但感觉就像一个简单的迂回方式.有没有其他方式我可以优雅地实现这一目标?
推论问题:
1)如果UIModalPresentationOverCurrentContext是实现这一目标的唯一方法,那么我想知道是否会被迫放置两个版本的代码以使其在iOS7和iOS8中都能正常工作.
2)显然,在早期版本的Xcode中无法识别新的枚举.那么,我的团队是否应该升级到Xcode 6以确保他们可以运行此代码,即使他们的其余工作仅以iOS7为中心?或者有没有办法告诉旧版本的Xcode忽略仅适用于iOS8的特定代码块?
我想要一个透明背景颜色的模态视图,以便用户可以看到它背后的视图.
看到蓝色不适合我的UI界面,我只是想知道是否有办法改变uialertview的颜色,或者改为使用图像.有了所有按钮,'解雇'等仍然存在
谢谢
我有一个uitextfield写的日期..如何在页面底部显示一个uidatepicker而不是标准键盘?提前致谢
我正在尝试使用只有一半高度的视图控制器
[self presentModalViewController:menu animated:YES];
Run Code Online (Sandbox Code Playgroud)
问题是当它出现时,视图控制器结束与屏幕相同的大小.我也尝试将"菜单"设置为屏幕的全尺寸,并将视图的透明度更改为白色,但这也不起作用.
我用一个视图显示presentModalViewController.从这个UIView我想推动UIView使用UINavigationController.我尝试下面的代码
[self.parentViewController.navigationController
pushViewController:objViewFullScreen
animated:YES];
Run Code Online (Sandbox Code Playgroud)
但它对我不起作用.所以请任何人建议我如何推动视图ModelViewController.
谢谢
iphone objective-c uinavigationcontroller presentmodalviewcontroller ios
将帐户添加到Mail(在首选项中)时,您将获得模态视图,如下所示:

我的问题是,我如何以编程方式复制这个?换句话说,如何在呈现视图上显示模态UIView?
这就是我所拥有的:
import UIKit
class ViewController: UIViewController {
@IBAction func addCard(sender: AnyObject) {
var addContact : secondViewController = secondViewController()
self.modalTransitionStyle = UIModalTransitionStyle.FlipHorizontal
self.modalPresentationStyle = .CurrentContext // Display on top of current UIView
self.presentViewController(addContact, animated: true, completion: nil)
}
//Code goes on to do other unrelated things
Run Code Online (Sandbox Code Playgroud)
另外,我做了以下事情:
预期的行为是,当按下UIBarButton"Add Contact"(@IBAction func addCard(sender: AnyObject)在上面的代码中成功触发 )时,secondViewController将呈现为主视图上方的模态视图.
当我运行上面的代码时,我收到错误 "Use of undeclared type secondViewController"
我究竟做错了什么?
注意:这是对早期问题的重新提问,其中我问了一个类似但略有不同的问题.我检查了meta,我认为它没关系 - 我不想让原始问题的答案无效,因为这是在问一些稍微不同的东西.另外,如果它有帮助,我发现了一些类似的问题 - …
我想创建一个黑色背景的模态视图控制器,但我想将alpha设为0.9,所以我仍然能够部分地看到它背后的视图.我该怎么做呢?我知道我可以使用UIView然后将其添加为子视图,但是如果我有UINavigationController或UITabBarController,这将不会覆盖整个屏幕.寻找一些关于此的建议,据我所知,到目前为止我看到的解决方案从未处理过彩色背景..大多数人只想要一个透明的背景.
ios ×7
objective-c ×5
iphone ×4
swift ×3
ipad ×2
modal-dialog ×2
xcode ×2
alpha ×1
background ×1
cocoa-touch ×1
editing ×1
ios7 ×1
ios8 ×1
singleton ×1
transparent ×1
uicolor ×1
uidatepicker ×1
uikit ×1
uitextfield ×1