如何快速拉伸标题视图?

时间:2019-01-21 10:57:13

标签: swift uinavigationcontroller uinavigationbar

我为UINavigationItem创建了自定义标题视图。
而且我希望自定义视图可以扩展到右填充。
我该怎么办?

我试图将rightBarButtonItem设置为nil。
但这似乎没有解决办法。
并且不要向左backButton区域伸展。

// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(TrainingActivity.this);
builder.setTitle("Choose a climber");
builder.setCancelable(true);

// add a list
String[] projection = {ClimbersEntry._ID, ClimbersEntry.COLUMN_NAME};
final Cursor cursor = getContentResolver().query(
        ClimbersEntry.CONTENT_URI,
        projection,
        null,
        null,
        null,
        null);

builder.setMultiChoiceItems(
        cursor,
        ClimbersEntry._ID,
        ClimbersEntry.COLUMN_NAME,
        new DialogInterface.OnMultiChoiceClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
        if (isChecked) {
            cursor.moveToPosition(which);
            addChoiceToJson(cursor.getInt(0));
        } else if (!isChecked){
            cursor.moveToPosition(which);
            String climberId = String.valueOf(cursor.getInt(0));
            // Loop to find the index of an item in climberArrayList
            for (int i = 0; i < climberArrayList.size(); i++) {
                Climber climber = climberArrayList.get(i);
                if (climberId.equals(String.valueOf(climber.getId()))) {
                    climberArrayList.remove(i);
                    break;
                }
            }
            trainingJsonObject.remove(climberId);
            trainingAdapter.notifyDataSetChanged();
        }
    }
});

// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();

查看我的结果。我想要一个红色区域。

https://imgur.com/a/dar3cMN

2 个答案:

答案 0 :(得分:0)

在CustomSearchBar类中尝试以下操作:

override var intrinsicContentSize: CGSize {
    return UILayoutFittingExpandedSize
}

答案 1 :(得分:0)

让searchBar = CustomSearchBar()

searchBar.translatesAutoresizingMaskIntoConstraints = false

self.navigationItem.titleView = searchBar

searchBar.heightAnchor.constraint(equalToConstant:30.0).isActive = true

searchBar.rightAnchor.constraint(equalTo:self.navigationController!.navigationBar.rightAnchor).isActive = true

searchBar.leftAnchor.constraint(equalTo:self.navigationController!.navigationBar.centerXAnchor).isActive = true

上面的代码对我有用。如果不需要中间的横条,则可以删除左锚点。我也将translatesAutoresizingMaskIntoConstraints设置为false,我希望这能起作用。