标签: programmatically

没有界面生成器的 Apple Metal

我正在为这个绞尽脑汁。我一直在尝试复制 Apple 提供的初始金属项目,但根本没有使用界面构建器。我能够创建一个窗口,但它是空白的,没有任何渲染,我不知道为什么。

主文件

#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSApplication* app = [NSApplication sharedApplication];
        AppDelegate* appDelegate = [[AppDelegate alloc] init];

        [app setDelegate:appDelegate];

        [app run];
    }

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

AppDelegate.h

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
    NSWindow *window;
}

@end
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m

#import <Metal/Metal.h>

#import "AppDelegate.h"

#import "GameViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (id)init {
    if (self = [super init]) {
        NSScreen* mainScreen = [NSScreen mainScreen];

        NSRect frame = …
Run Code Online (Sandbox Code Playgroud)

macos xcode objective-c metal programmatically

4
推荐指数
1
解决办法
1058
查看次数

从UIAlertController以编程方式打开蓝牙设置

标题是,问题是如何从我的应用程序以编程方式打开蓝牙设置?我检查了stackoverflow上的所有答案,甚至没有一个在iOS 11中工作正常.所有这些都是旧的答案是低于11或没有答案.我所能做的只是带我进入常规设置或应用程序设置,但我希望用户可以直接进入蓝牙设置,这样他就可以打开它.

let alert = UIAlertController(title: "Bluetooth is off", message: "Please turn on your bluetooth", preferredStyle: UIAlertControllerStyle.alert)
            alert.addAction(UIAlertAction(title: "Go to settings", style: UIAlertActionStyle.default, handler: { (action) in
                switch action.style {
                case .default:
                    let url = URL(string: "App-Prefs:root=Bluetooth") //for bluetooth setting
                    let app = UIApplication.shared
                    app.openURL(url!)
                    print("default")
                case .cancel:
                    print("cancel")
                case .destructive:
                    print("destrucive")
                }
Run Code Online (Sandbox Code Playgroud)

我尝试了更多,也没有结果:

UIApplication.shared.openURL(NSURL(string: "prefs:root=General&path=Bluetoth")! as URL)
Run Code Online (Sandbox Code Playgroud)

要么:

let url = URL(string: "App-Prefs:root=Bluetooth")
Run Code Online (Sandbox Code Playgroud)

以上都不起作用

bluetooth ios swift programmatically

4
推荐指数
1
解决办法
5977
查看次数

如何使中心和宽高比适合Swift 3中的图像?

我在Swift 3中有一个项目,我在视图上使用渐变.我在视图上放置了一个图像,但它位于渐变层下面,所以我打算以编程方式而不是故事板绘制图像.我似乎无法弄清楚如何使图像居中并且在视图的边界内适应方面.

是)我有的...

//Gradient Background
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.view.frame
gradientLayer.colors = [UIColor(red: 0/255, green: 147/255, blue: 255/255, alpha: 1.0).cgColor, UIColor(red: 162/255, green: 134/255, blue: 255/255, alpha: 1.0).cgColor]
gradientLayer.locations = [0.0, 1.0]
gradientLayer.startPoint = CGPoint(x: 0.0, y: 1.0)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.0)
self.view.layer.addSublayer(gradientLayer)

//Image
let mainImage = UIImage(named:"WhiteLogoFront")
var mainImageView = UIImageView(image:mainImage)
self.view.addSubview(mainImageView)
Run Code Online (Sandbox Code Playgroud)

image ios swift programmatically

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

更改代码中的大小类(快速)

我一直在尝试以纯代码(不使用情节提要)构建自适应布局并开始运行。我使用布局锚来设置约束,并利用traitCollectionDidChange方法在各种约束集和其他接口更改之间进行切换。我使用switch语句使用相应的约束集调用相应的方法。

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    super.traitCollectionDidChange(previousTraitCollection)

    switch (traitCollection.horizontalSizeClass, traitCollection.verticalSizeClass) {
    case (.regular, .regular):
        setupRegularRegular()

    case (.compact, .compact):
        setupCompactCompact()

    case (.regular, .compact):
        setupRegularCompact()

    case (.compact, .regular):
        setupCompactRegular()

    default: break
    }

}
Run Code Online (Sandbox Code Playgroud)

为了进行测试,我仅更改了一个约束,即centerYAnchor.constraint。在纵向模式中,常量为-150,在横向模式中,我希望将其更改为50。对于iPad,我将常量设置为0。

bigLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -150).isActive = true
Run Code Online (Sandbox Code Playgroud)

在iPhone和iPad之间切换时,效果很好。但是,如果您开始将iPhone从纵向旋转到横向,则布局系统将失败,并显示:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find …
Run Code Online (Sandbox Code Playgroud)

ios swift size-classes adaptive-layout programmatically

3
推荐指数
1
解决办法
2546
查看次数

如何以编程方式设置或重置复选框

目的:当检查或未选中特定的复选框时,程序必须根据程序化规则更改许多其他复选框。

<input type="checkbox" id="chkbox" (change)="onChangeChk($event)">Test Checkbox

  onChangeChk($event) {
    $event.srcElement.value = "on"; // or "off"
  }
Run Code Online (Sandbox Code Playgroud)

无论 onChangeChk 如何设置复选框,该复选框都将保持其原始状态。

感谢您在这方面的帮助。:-)

checkbox programmatically angular

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

UITextView在键入/不使用故事板时动态更改的高度

我有这个UITextView&我希望它的高度在用户输入时动态改变.我想以编程方式进行.我在另一个UIView上面有UITextView.约束设置如下:

 addtextview.leadingAnchor.constraint(equalTo: addtasktextview.leadingAnchor, constant: 8).isActive = true
      addtextview.trailingAnchor.constraint(equalTo: addtasktextview.trailingAnchor, constant: -8).isActive = true
      addtextview.topAnchor.constraint(equalTo: addtasktextview.topAnchor, constant: 8).isActive = true
      addtextview.bottomAnchor.constraint(equalTo: addtasktextview.bottomAnchor, constant: -40).isActive = true
Run Code Online (Sandbox Code Playgroud)

有趣的是,我也在tableViewCells中使用textview并使用这种约束方法动态地改变高度,但是在这里它不起作用.我希望textview的高度以向上移动的方式增加.因此,当新线开始时,顶部应移动以保持下面的间距.

我该怎么做?帮助将不胜感激.

在此输入图像描述

更新:我能够使用下面的@ upholder-of-truth的答案.我还能够通过查找textview normal height和newSize.height之间的差异,然后将该差异添加到容器的高度来动态更改父UIView容器高度.

height textview ios swift programmatically

3
推荐指数
2
解决办法
5167
查看次数

如何动态删除Vue过渡

我有一个表体,它被声明为动画块。在某些特定情况下,我想删除到表格主体的过渡。有什么办法可以做到这一点。

transition vue.js programmatically

3
推荐指数
1
解决办法
1805
查看次数

以编程方式在 Bitbucket 上创建拉取请求?

更新:嗯,我在 Bash 脚本中运行它,但我想看看我得到了什么错误代码,现在我可以看到我得到了一个401 Unauthorized. 我正在使用我的用户名并使用admin访问 bitbucket创建了个人访问令牌,所以我应该能够创建一个 PR 对吗?我可以通过同一存储库上的 Web UI 执行此操作吗?

我正在运行 bash 脚本以在 Bitbucket 上创建拉取请求。我已经在以编程方式克隆 repo、编辑文件、执行 git add/commit,现在我只需要使用 CURL 来制作 PR。似乎 bitbucket API 公开了一个端点来使用 POST 请求执行此操作:

Creates a new pull request where the destination repository is this repository and the author is the authenticated user.

The minimum required fields to create a pull request are title and source, specified by a branch name.

curl https://api.bitbucket.org/2.0/repositories/my-username/my-repository/pullrequests \
    -u my-username:my-password \
    --request POST \
    --header 'Content-Type: …
Run Code Online (Sandbox Code Playgroud)

git bitbucket pull-request programmatically

3
推荐指数
1
解决办法
3051
查看次数

Android 以编程方式设置多个方向

我尝试以编程方式设置多个方向。例如我想屏幕方向可以是纵向、反向纵向和反向横向。

我尝试使用此代码,但似乎不起作用。

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
Run Code Online (Sandbox Code Playgroud)

编辑

setRequestedOrientation 方法适用于最新的方向。我只是试图找到一种方法来设置两个方向。

android orientation programmatically

2
推荐指数
1
解决办法
1714
查看次数

以编程方式添加负约束

为什么我必须给我的rigthAnchor约束一个负值才能titleLabel从视图的右侧产生 28 个边距。如果我只是给它正值,它会超出视图 28 个像素。

private func setupLayout() {
    view.addSubview(imageView)
    view.addSubview(titleLabel)

    // *** imageView ***
    if #available(iOS 11.0, *) {
        imageView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 116).isActive = true
    } else {
        imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 116).isActive = true
    }
    imageView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    imageView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    imageView.heightAnchor.constraint(equalToConstant: 224).isActive = true

    // *** titleLabel ***
    titleLabel.topAnchor.constraint(equalTo: imageView.bottomAnchor, constant: 25).isActive = true
    titleLabel.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -28).isActive = true
    titleLabel.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 28).isActive = true
}
Run Code Online (Sandbox Code Playgroud)

anchor xcode constraints swift programmatically

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