te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>swift - Image changing the HStack behavior - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - Image changing the HStack behavior - Stack Overflow

programmeradmin3浏览0评论

I'm trying to do some simple buttons, a button with or without a lock after it. The problem is when I add the Spacer(), it work correctly, but when I use an Image, the button goes to the left.

HStack() {
  Spacer()
  ZStack {
    buttonBackground(isLocked: isLocked)
    Text(level)
      .foregroundColor(isLocked ? Color.gray : Color.white)
      .frame(maxWidth: .infinity, alignment: .center)
  }
  .frame(width: screenWidth/2, height: 40)
                
   if isLocked {
     Image(systemName: "lock.fill")
       .resizable()
       .foregroundColor(.gray)
       .frame(width: 30, height: 30, alignment: .topLeading)
    } else {
      Spacer()
    }
}

Button with and without lock:

I can't make the button to stay in the middle when the Image is added.

I'm trying to do some simple buttons, a button with or without a lock after it. The problem is when I add the Spacer(), it work correctly, but when I use an Image, the button goes to the left.

HStack() {
  Spacer()
  ZStack {
    buttonBackground(isLocked: isLocked)
    Text(level)
      .foregroundColor(isLocked ? Color.gray : Color.white)
      .frame(maxWidth: .infinity, alignment: .center)
  }
  .frame(width: screenWidth/2, height: 40)
                
   if isLocked {
     Image(systemName: "lock.fill")
       .resizable()
       .foregroundColor(.gray)
       .frame(width: 30, height: 30, alignment: .topLeading)
    } else {
      Spacer()
    }
}

Button with and without lock:

I can't make the button to stay in the middle when the Image is added.

Share Improve this question edited Feb 17 at 21:28 Benzy Neez 21.2k3 gold badges14 silver badges36 bronze badges asked Feb 17 at 20:53 KelvinKelvin 11 bronze badge New contributor Kelvin is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 0

When there is no image, the Spacer on both sides of the button are keeping it centered. However, when the image is shown, there is a Spacer on the left and the image on the right, so this is not likely to be balanced.

It's interesting that the button is pushed to the left when the image is present. If the full screen width would be available for showing the button and image then you would expect the Spacer on the left to be pushing the content to the right. My guess is that there must be a VStack that contains the buttons and this is being constrained to the width of the Menu button. This means, the Spacer are only filling a very small width.

I would suggest these changes:

  • Remove the HStack and remove the Spacer. That just leaves the ZStack and the if/else with the Image.
  • Add horizontal padding on both sides of the ZStack to reserve space for showing the image, if needed.
  • Show the image as an overlay with alignment: .trailing.
  • If you are setting a .frame with a fixed width on the VStack that contains the buttons then remove this too. This might mean, you will need to add horizontal padding to the Menu button, or you could set a fixed width on the Menu button.
ZStack {
    buttonBackground(isLocked: isLocked)
    Text(level)
        .foregroundStyle(isLocked ? .gray : .white)
}
.frame(width: screenWidth/2, height: 40)
.padding(.horizontal, 40)
.overlay(alignment: .trailing) {
    if isLocked {
        Image(systemName: "lock.fill")
            .resizable()
            .foregroundStyle(.gray)
            .frame(width: 30, height: 30)
    }
}


You may be using a GeometryReader to measure the screen width. If so, the content inside the GeometryReader will be aligned to the top-leading corner. To align the content to the center of the screen, add a frame with maxWidth: .infinity, maxHeight: .infinity:

GeometryReader { proxy in
    let screenWidth = proxy.size.width
    VStack {
        buttons(screenWidth: screenWidth)
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
}
发布评论

评论列表(0)

  1. 暂无评论