小编Ale*_*kov的帖子

iOS:以编程方式更改Interface Builder中设置的约束

美好的一天,朋友们!

我没有尝试动画从代码中设置在IB中的视图.应用崩溃的原因如下:

视图层次结构没有为约束准备......

我在这里看到了一些类似的问题,原因始终是以编程方式创建的视图尚未添加到superview中.但我在IB中创建了所有观点!

控制台还说:

在容器层次结构中找不到视图:( 这里是超级视图)

它对我没有任何意义,因为事实上它是适当的superview的子视图,xcode知道它 - 它立即打印视图层次结构,它适合.

可能是什么原因?谢谢!

编辑:我使用的代码:

    - (void)setEditingConstraintsForView:(UIView *)view
{
    // Pin given view to top, fix it height
    NSDictionary *givenView = @{@"view":view};
    view.translatesAutoresizingMaskIntoConstraints = NO;

    NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:givenView];
    NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view(height)]" options:0 metrics:@{@"height":@(viewHeight)} views:givenView];

    [self.animatedVIew addSubview:view];

    for (NSArray *constraints in @[horizontalConstraints, verticalConstraints]) {
        [view addConstraints:constraints];
    }
}
Run Code Online (Sandbox Code Playgroud)

我还删除了在安装new之前在IB中设置的所有约束:

    - (NSDictionary *)constraintsFromIB
{
    if (!_constraintsFromIB) {
        _constraintsFromIB = @{@"view1":self.view1.referencingConstraintsInSuperviews,
                               @"view2":self.view2.referencingConstraintsInSuperviews,
                               @"view3":self.view3.referencingConstraintsInSuperviews };
    }

    return _constraintsFromIB;
}
Run Code Online (Sandbox Code Playgroud)

然后: …

objective-c ios autolayout nslayoutconstraint

8
推荐指数
1
解决办法
1万
查看次数

UIViewController 没有释放自己

我正在开发一个应用程序,根据它的状态更改它的 rootViewController。要进行切换,我使用以下代码:

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self createManagedDocumentAndContext];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *storyboardId = [userDefaults boolForKey:@"Profile Created"] ? @"User Stats" : @"Profile";

    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardId];

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

要切换回来,我从提供的 ProfileVC 调用此方法:

- (void)returnOldRootViewController
{
    UIWindow *currentWindow = [UIApplication sharedApplication].keyWindow;

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OLDUserStatsVC *userStatsVC = [storyboard instantiateViewControllerWithIdentifier:@"User Stats"];
    userStatsVC.userProfile = self.userProfile;

    [currentWindow setRootViewController:userStatsVC];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:@"Profile Created"];

}
Run Code Online (Sandbox Code Playgroud)

问题: rootViewController 已更改,但前一个未释放。它停留在应用程序的“背景”上——当 VC 更改为另一个时,我可以看到它。

问题是如何正确释放它?非常感谢!

objective-c uiviewcontroller ios automatic-ref-counting

5
推荐指数
1
解决办法
1275
查看次数

Nib文件加载速度极慢

美好的一天朋友!

我在加载Nib文件时有非常奇怪的性能结果.以下是TimeProfiler的屏幕:

在此输入图像描述

总的来,加载一个有2个笔尖的简单viewController有时需要1200多毫秒.笔尖非常简单.这是一个例子.从上面的屏幕截图中可以看出,加载需要311毫秒:

在此输入图像描述

我用来加载nib的代码:

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    setupXib()
}

func setupXib() {
    NSBundle.mainBundle().loadNibNamed("TimeSaver", owner: self, options: nil)
    bounds = view.bounds
    addSubview(view)
    setup()
}

func setup() {
    slider.maximumValue = 30.0
    slider.minimumValue = 1.0
    slider.value = 5.0
    timeLabel.text = "\(sliderValue.format(format)) min"
}
Run Code Online (Sandbox Code Playgroud)

我不知道它是否会导致问题,但此应用程序使用App Groups(用于WatchExtension).如你所见,我从mainBundle调用nibs,因为它们都是主要目标.可能是原因吗?

谢谢!

nib ios

5
推荐指数
1
解决办法
1201
查看次数