我有用作模式授权对话框的 NSWindows 实现。编码:
using System;
using AppKit;
using Cairo;
namespace SomeNameSpace
{
public class DialogWindow : NSWindow
{
public DialogWindow() : base()
{
var stackView = new NSStackView(){
AutoresizingMask = NSViewResizingMask.HeightSizable | NSViewResizingMask.WidthSizable,
};
stackView.WantsLayer = true;
stackView.Layer.BackgroundColor = new CoreGraphics.CGColor(255f, 0f, 0f);
stackView.Alignment = NSLayoutAttribute.Leading;
stackView.Orientation = NSUserInterfaceLayoutOrientation.Vertical;
ContentView = stackView;
SetFrame(new CoreGraphics.CGRect(Frame.Location, new CoreGraphics.CGSize(300, 210)), true);
StyleMask |= NSWindowStyle.Closable;
}
public void ShowDialog()
{
NSApplication.SharedApplication.RunModalForWindow(this);
}
}
public class AuthDialog : DialogWindow
{
public AuthDialog() : base()
{
var usernameLabel = new NSTextField() {
StringValue = "Username",
Editable = false,
DrawsBackground = false,
Selectable = false,
Bezeled = false,
};
usernameLabel.SizeToFit();
var usernameEditor = new NSTextField();
var passwordLabel = new NSTextField() {
StringValue = "Password",
Editable = false,
DrawsBackground = false,
Selectable = false,
Bezeled = false,
};
passwordLabel.SizeToFit();
var passwordEditor = new NSSecureTextField();
var actionButtonsView = new NSStackView() {
Orientation = NSUserInterfaceLayoutOrientation.Horizontal,
};
actionButtonsView.WantsLayer = true;
actionButtonsView.Layer.BackgroundColor = new CoreGraphics.CGColor(0f, 255f, 0f);
var cancelButton = new NSButton() {
Title = "Cancel",
};
var loginButton = new NSButton() {
Title = "Login",
};
actionButtonsView.TranslatesAutoresizingMaskIntoConstraints = false;
actionButtonsView.AddArrangedSubview(cancelButton);
actionButtonsView.AddArrangedSubview(loginButton);
loginButton.SizeToFit();
var stackView = (NSStackView)ContentView;
stackView.AddArrangedSubview(usernameLabel);
stackView.AddArrangedSubview(usernameEditor);
stackView.AddArrangedSubview(passwordLabel);
stackView.AddArrangedSubview(passwordEditor);
stackView.AddArrangedSubview(actionButtonsView);
ShowsResizeIndicator = false;
}
}
}
它只是创建基于 NSStackView 的布局。看起来像这样:
问题是如何将最后一个子视图(包含按钮)对齐到父视图(和窗口)的右侧?编程语言无关紧要。它很容易翻译成 swift\obj-c,反之亦然。